0

i am new to websocket using pywebsocket, basically trying to create a simple chat application with websocket and pywebsocket. Until now i am done with following script

  if ("WebSocket" in window)
  {
     //alert("WebSocket is supported by your Browser!");
     // Let us open a web socket
     var ws = new WebSocket("ws://192.168.1.3:9998/echo");
     ws.onopen = function()
     {
        // Web Socket is connected, send data using send()
        console.log("Channel opened");
        //ws.send("Message to send");
        //alert("Message is sent...");
        //console.log("Message is sent...");
     };
     ws.onmessage = function (evt) 
     { 
        var received_msg = evt.data;
        //alert("Message is received..."+received_msg);
        console.log("message : "+received_msg);
     };
     ws.onclose = function()
     { 
        // websocket is closed.
        //alert("Connection is closed...");
        console.log("connection closed");
     };
  }
  else
  {
     // The browser doesn't support WebSocket
     alert("WebSocket NOT supported by your Browser!");
  }

  function sendmessage(){
    msg=document.getElementById("chat").value;
    console.log(msg);
    ws.send(msg);
    document.getElementById("chat").value="";
  }

Now the problem is the messages that are sent are echoed to the system itself, if another client connect to same channel its messages are echoed to itself, they are sent to another client which is connected with same channel.

pywebsocket is initialized as follows python standalone.py -p 9998 -w ../example/

So how can i connect two system and allow chat. Thanks in advance

Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56
  • look like you don't keep a list of connections anywhere. You send data to the same connection. Some py code may be useful, but basicaly you have to keep all connection in some placeholder and answer to each – cox Oct 02 '13 at 06:17

1 Answers1

0

Not sure, but if you want to broadcast any received message (sent by any connected client) to all other currently connected clients, that is not "echo" .. but "broadcast".

Here is an example using AutobahnPython.

Disclosure: I am original author of Autobahn and work for Tavendo.

oberstet
  • 21,353
  • 10
  • 64
  • 97