Last active
August 29, 2015 14:13
-
-
Save kshirish/c789d06573c503a8221d to your computer and use it in GitHub Desktop.
Websocket and Node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//////////////////////////////////////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