我是使用 pywebsocket 的 websocket 新手,基本上是尝试使用 websocket 和 pywebsocket 创建一个简单的聊天应用程序。直到现在我完成了以下脚本
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="";
}
现在的问题是发送的消息被回显到系统本身,如果另一个客户端连接到同一个通道,它的消息被回显到自己,它们被发送到另一个连接到相同通道的客户端。
pywebsocket 初始化如下 python Standalone.py -p 9998 -w ../example/
那么我如何连接两个系统并允许聊天。提前致谢