Skip to content

Instantly share code, notes, and snippets.

@kshirish
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kshirish/c789d06573c503a8221d to your computer and use it in GitHub Desktop.
Save kshirish/c789d06573c503a8221d to your computer and use it in GitHub Desktop.
Websocket and Node
//////////////////////////////////////Server////////////////////////////////////////
var WebSocketServer = require("ws").Server;
var http = require("http");
var express = require("express");
var app = express();
var port = process.env.PORT || 5000;
var server = http.createServer(app);
server.listen(port);
var wss = new WebSocketServer({server: server});
wss.on("connection", function(ws) {
console.log("Connected !");
ws.on("message", function(data){
console.log('Message from client: '+ data);
});
ws.on("close", function() {
console.log("websocket connection close")
clearInterval(id)
});
})
//////////////////////////////////////Client////////////////////////////////////////
var host = 'ws://localhost:5000';
var ws = new WebSocket(host);
ws.onmessage = function (event) {
console.log('Message from Server: '+ event.data);
};
ws.send('Sending test message from client');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment