hello world demo
-
server side
-
install
ws
library$ npm install ws
-
nodejs code
server.js
var WebSocketServer = require('ws').Server, wss = new WebSocketServer({port: 8181}); wss.on('connection', function(ws) { console.log('client connected'); ws.on('message', function(message) { console.log(message); ws.send(message) }); ws.onclose = function(e) { console.log(e.code + "" + e.reason); } })
-
run the server
$ node server.js
-
-
client side
-
install python websocket-client
$ pip install websocket-client
-
client code
client.py
#! coding: utf-8 from websocket import create_connection def send_msg(msg): """send_msg""" ws = create_connection("ws://localhost:8181/") print "client> {}".format(msg) ws.send(msg) print "server< {}".format(ws.recv()) ws.close() send_msg('hello world')
-