Skip to content

Instantly share code, notes, and snippets.

@rectangletangle
Created October 17, 2016 20:23
Show Gist options
  • Save rectangletangle/3ad486daa8db3cd43719031840776caf to your computer and use it in GitHub Desktop.
Save rectangletangle/3ad486daa8db3cd43719031840776caf to your computer and use it in GitHub Desktop.
// Note that the path doesn't matter for routing; any WebSocket
// connection gets bumped over to WebSocket consumers
socket = new WebSocket("ws://127.0.0.1:7000/chat/");
socket.onmessage = function(e) {
alert(e.data);
}
socket.onopen = function() {
socket.send("hello world");
}
// Call onopen directly if socket is already open
if (socket.readyState == WebSocket.OPEN) socket.onopen();
//Socket.io
var socket = io.connect();
// React to a received message
socket.on('ping', function (data) {
// Modify the DOM to show the message
document.getElementById("msg").innerHTML = data.msg;
// Send a message back to the server
socket.emit('pong', {
msg: "The web browser also knows socket.io."
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment