Web Socket Example

This is an example Demonstrating the use of this library


In [1]:
import websock
import threading

class MySocketHandler(websock.WebSocketDelegate):
    def OnConnect(self):
        print("connected")
        self.socket.send("Hi!")
    def OnRecieve(self,data):
        print("Got Message {}".format(data))
websock.run_server_in_bg(MySocketHandler)

Now Let's test with chrome


In [3]:
%%javascript
console.log("creating WS")
var exampleSocket = new WebSocket("ws://127.0.0.1:9999");
exampleSocket.onopen = function (event) {
    console.log("WS connected")
  exampleSocket.send("Here's some text that the server is urgently awaiting!"); 
};
exampleSocket.onmessage = function (event) {
  alert(event.data);
}


using v13
connected
Got Message b"Here's some text that the server is urgently awaiting!"

In [ ]: