Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- server.js
- -----
- var io = require('socket.io'),
- socket = io.listen(8080);
- socket.on('connection', function(client)
- {
- // now we have a client object!
- console.log("\r\nConnection accepted.\r\n");
- client.on('message', function(message)
- {
- console.log("\r\nRecieved message: " + message + " - from client " + client.sessionId + "\r\n");
- });
- client.on('disconnect', function()
- {
- console.log("\r\nConnected " + client.sessionId + "terminated.\r\n");
- });
- });
- index.html (served by apache)
- -----
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>Comet Test</title>
- </head>
- <body>
- <h1>Socket.io Test</h1>
- <div><p id="status">Waiting for input</p></div>
- <button id="connect" onClick="connect();"/>Connect</button>
- <button id="disconnect" onClick="disconnect();">Disconnect</button>
- <button id="send" onClick="send();" />Send Message</button>
- <script src="http://houseofellis.net:8080/socket.io/socket.io.js" type="text/javascript"></script>
- <script type="text/javascript">
- function connect()
- {
- socket.connect('houseofellis.net', {port: 8080, rememberTransport: false});
- }
- function disconnect()
- {
- socket.disconnect();
- }
- function message(data)
- {
- document.getElementById('message').value = data;
- }
- function status_update(txt)
- {
- document.getElementById('status').innerHTML = txt;
- }
- function esc(msg)
- {
- return msg.replace(/</g, '<').replace(/>/g, '>');
- }
- function send()
- {
- socket.send("Hello Server!");
- }
- // Start the socket
- var socket = io.connect('houseofellis.net', {port: 8080, rememberTransport: false});
- socket.on('message', function(data){ message(data); });
- socket.on('connect', function(){ status_update("Connected"); });
- socket.on('disconnect', function(){ status_update("Disconnected"); });
- socket.on('reconnect', function(){ status_update("Reconnected"); });
- socket.on('reconnecting', function( nextRetry ) {
- status_update("Reconnecting in " + nextRetry + " seconds"); });
- socket.on('reconnect_failed', function(){ message("Reconnect Failed"); });
- </script>
- </body>
- Server console when I click on the message button
- ----
- debug - setting request POST /socket.io/1/xhr-polling/542daf353bd7d06cde45d7800118f9a6?t1308741555720
- debug - transport not writeable, not subscribing
- debug - xhr received data packet 3:::Hello Server!
- debug - got packet
Advertisement
Add Comment
Please, Sign In to add comment