2013-10-02 1 views
0

pywbsocket을 사용하여 websocket을 처음 사용합니다. 기본적으로 websocket 및 pywebsocket을 사용하여 간단한 채팅 응용 프로그램을 만들려고합니다. 는 지금까지 내가 스크립트를 다음과 같이 수행하고PyWebSocket을 사용하여 websocket에 두 클라이언트를 연결하십시오.

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=""; 
    } 

이제 문제는 또 다른 클라이언트가 메시지 자체에 에코됩니다 같은 채널에 연결하는 경우 , 시스템 자체에를 에코됩니다 보낸 메시지입니다 동일한 채널로 연결된 다른 클라이언트로 전송됩니다. -w ../example/

9998 -p 파이썬 standalone.py을 다음과 같이

pywebsocket 그래서 내가 어떻게 두 시스템을 연결하고 채팅을 할 수 있습니다 초기화됩니다. 미리 감사드립니다.

+0

어디서나 연결 목록을 유지하지 않는 것처럼 보입니다. 데이터를 동일한 연결로 보냅니다. 일부 py 코드가 유용 할 수 있지만 근본적으로 모든 연결을 일부 자리 표시 자에 유지하고 각 자리 표시 자에 응답해야합니다 – cox

답변

0

잘 모르겠지만 (연결된 클라이언트가 보낸) 수신 된 모든 메시지를 "에코"가 아닌 "브로드 캐스트"로 브로드 캐스트하고 싶다면.

여기는 example using AutobahnPython입니다.

공개 : 나는 Autobahn의 원작자이며 Tavendo에서 일합니다.