24 February 2016

hello world demo

  1. server side

    1. install ws library

       $ npm install ws
      
    2. 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);
           }
       })
      
    3. run the server

       $ node server.js
      
  2. client side

    1. install python websocket-client

       $ pip install websocket-client
      
    2. 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')
      


blog comments powered by Disqus