Skip to content

Instantly share code, notes, and snippets.

@Rob-pw
Last active January 2, 2016 14:29
Show Gist options
  • Save Rob-pw/8317292 to your computer and use it in GitHub Desktop.
Save Rob-pw/8317292 to your computer and use it in GitHub Desktop.
/*
If socket.io is running on port 80, here is the shorthand script tag
<script src="/socket.io/socket.io.js"></script>
Else, you'll have to specify the port and host.
<script src="http://localhost:8080/socket.io/socket.io.js"></script>
*/
//If port 80
//var socket = io.connect();
var socket = io.connect('http://localhost:8080');
//When we receive the event 'motd' from the server, run the callback passing the data
socket.on('motd', function(data) {
//In this demo, the message "Welcome to my server!" will be alerted.
alert("MOTD: " + data);
});
//Emits an event 'message', with the data "'ello friends, I am here."
socket.emit('message', "'ello friends, I am here.");
//Emits an event, passes some data and calls a function once a response is received.
socket.emit('important_message', {
alias : "bookfort",
message : "This is bookfort to server, do you read me? Over."
}, function(data) {
console.log(data);
});
//Tells the server that we're disconnecting, raises the 'disconnect' event.
socket.disconnect();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment