Skip to content

Instantly share code, notes, and snippets.

@rectangletangle
Created October 17, 2016 20:23

Revisions

  1. rectangletangle created this gist Oct 17, 2016.
    23 changes: 23 additions & 0 deletions ws-socket.io boilerplate
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    // 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."
    });
    });