在浏览器上使用websocket客户端的tcl-server

时间:2018-01-06 15:47:15

标签: javascript websocket tcl client-server

我是WebSockets的新手。我想设置一个tcl-server和websocket-browser -client。

echoServer.tcl

#!/usr/bin/tclsh
proc Echo_Server {} {
    global echo
    set echo(main) [socket -server EchoAccept 1617]
    puts "I'm waiting... on the port [lindex [fconfigure $echo(main) -sockname] end]"
    vwait events

}
proc EchoAccept {sock addr port} {
    global echo
    puts "Accept $sock from $addr port $port"
    set echo(addr,$sock) [list $addr $port]
    fconfigure $sock -buffering line
    fileevent $sock readable [list Echo $sock]
}
proc Echo {sock} {
    global echo
    if {[eof $sock] || [catch {gets $sock line}]} {
        # end of file or abnormal connection drop
        close $sock
        puts "Close $echo(addr,$sock)"
        unset echo(addr,$sock)
    } else {
        puts "I received $line"
        if {[string compare $line "quit"] == 0} {
            # Prevent new connections.
            # Existing connections stay open.
            close $echo(main)
        }
        puts $sock $line
    }
}
Echo_Server 

来自浏览器的WebSocket代码如下,

var ws = new WebSocket("ws://myserver:1617");

ws.onopen = function()
{
 // Web Socket is connected, send data using send()
  ws.send("Message to send");
  alert("Message is sent...");
};

ws.onmessage = function (evt) 
{ 
  var received_msg = evt.data;
  alert("Message is received...");
};

ws.onclose = function()
{ 
  // websocket is closed.
  alert("Connection is closed..."); 
};

window.onbeforeunload = function(event) {
  socket.close();
};

如果我运行客户端WebSocket代码,那么它将直接转到onclose方法。在控制台中,我可以看到以下错误。

VM54:1 WebSocket connection to 'ws://myserver:1617/' failed: Error during WebSocket handshake: net::ERR_INVALID_HTTP_RESPONSE

服务器的输出如下,

I'm waiting... on the port 1617
Accept sock6 from xx.xx.xx.100 port 52825
I received GET / HTTP/1.1
I received Host: myserver:1617
I received Connection: Upgrade
I received Pragma: no-cache
I received Cache-Control: no-cache
I received Upgrade: websocket
I received Origin: http://wiki.tcl.tk
I received Sec-WebSocket-Version: 13
I received User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36
I received Accept-Encoding: gzip, deflate
I received Accept-Language: en-US,en;q=0.9
I received Sec-WebSocket-Key: fEoPHKVWt96GeRtMmOrOeQ==
I received Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
I received
I received
Close xx.xx.xx.100 52825

这有什么问题?

0 个答案:

没有答案