pubsub.html 2.87 KB
Newer Older
Sergey Lyubka's avatar
Sergey Lyubka committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>WebSocket Test</title>
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <style type="text/css">
    body {
      background-color: #cde; margin: 0;
      padding: 0; font: 14px Helvetica, Arial, sans-serif;
    }
    div.content {
      width: 800px; margin: 2em auto; padding: 20px 50px;
      background-color: #fff; border-radius: 1em;
    }
    #messages {
      border: 2px solid #fec; border-radius: 1em;
      height: 10em; overflow: scroll; padding: 0.5em 1em;
    }
    a:link, a:visited { color: #69c; text-decoration: none; }
    @media (max-width: 700px) {
      body { background-color: #fff; }
      div.content {
        width: auto; margin: 0 auto; border-radius: 0;
        padding: 1em;
      }
    }
</style>

<script language="javascript" type="text/javascript">

  var rooms = [];
  var ws = new WebSocket('ws://' + location.host + '/ws');

  if (!window.console) { window.console = { log: function() {} } };

  ws.onopen = function(ev)  { console.log(ev); };
  ws.onerror = function(ev) { console.log(ev); };
  ws.onclose = function(ev) { console.log(ev); };
  ws.onmessage = function(ev) {
    console.log(ev);
    var m = (ev.data || '').match(/^(\S+) (.+)/);
    if (m[1] == 'id') {
      document.getElementById('my_id').innerText = m[2];
    } else if (m[1] == 'msg') {
      var div = document.createElement('div');
      div.innerHTML = m[2];
      document.getElementById('messages').appendChild(div);
    }
  };
  
  window.onload = function() {
    document.getElementById('send_button').onclick = function(ev) {
      var msg = document.getElementById('send_input').value;
      ws.send('msg ' + msg);
    };
    document.getElementById('room_sel').onchange = function(ev) {
      var roomName = this.value || '?';
      ws.send('join ' + roomName);
    };
  };
</script>
</head>
<body>
  <div class="content">
    <h1>Websocket PubSub Demonstration</h1>

    <p>
      This page demonstrates how Mongoose web server could be used to implement
      <a href="http://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern">
       publish–subscribe pattern</a>. Open this page in several browser
       windows. Each window initiates persistent
       <a href="http://en.wikipedia.org/wiki/WebSocket">WebSocket</a>
      connection with Mongoose, making each browser window a websocket client.
      Join a room, send messages, and see messages sent by other clients.
    </p>
    
    <p>
      My ID: <b><span id="my_id"></b></span>
    </p>
    <p>
      Join room: <select id="room_sel">
        <option value="">-- select room -- </option>
        <option>A</option>
        <option>B</option>
      </select>
    </p>

    <div id="messages">
    </div>
    
    <p>
      <input type="text" id="send_input" />
      <button id="send_button">Send Message</button>
    </p>
  </div>
</body>
</html>