| Description: |
I've noticed Firefox always complain about "failed websocket connection" when using ::webslocket $socket close from the server side.
Reason:
set Connection(state) CLOSED
if { $code == "" || ![string is integer $code] } {
send $sock 8
but of course send does refuse to send anything while in state CLOSED ...
Furthermore, the server needs to wait for the client to complete the close handshake.
Proposed fix:
--- ../tmp/tcllib-tcllib_1_17/modules/websocket/websocket.tcl 2015-04-30 06:21:20.000000000 +0200
+++ websocket-tcllib.tcl 2015-10-22 19:23:01.727986813 +0200
@@ -113,6 +113,7 @@
# None.
proc ::websocket::Disconnect { sock } {
variable WS
+ variable log
set varname [namespace current]::Connection_$sock
upvar \#0 $varname Connection
@@ -120,6 +121,7 @@
if { $Connection(liveness) ne "" } {
after cancel $Connection(liveness)
}
+ ${log}::debug "disconnect $sock"
Push $sock disconnect "Disconnected from remote end"
catch {::close $sock}
unset $varname
@@ -155,13 +157,17 @@
if { $Connection(state) eq "CLOSED" } {
${log}::notice "Connection already closed"
+ if {$code != 1000} {
+ # possibly an error while waiting for close handshake => force disconnect
+ Disconnect $sock
+ }
return
}
- set Connection(state) CLOSED
if { $code == "" || ![string is integer $code] } {
send $sock 8
${log}::info "Closing web socket"
+ set Connection(state) CLOSED
Push $sock close {}
} else {
if { $reason eq "" } {
@@ -183,10 +189,10 @@
set msg [string range $msg 0 124]; # Cut answer to make sure it fits!
send $sock 8 $msg
${log}::info "Closing web socket: $code ($reason)"
+ set Connection(state) CLOSED
Push $sock close [list $code $reason]
}
-
- Disconnect $sock
+ # Keep Socket open to recieve close handshake!
}
@@ -869,11 +875,18 @@
# continuation frame, fragmented or oversized control frame, or
# the opcode is unrecognised.
if { [catch {read $sock 2} dta] || [string length $dta] != 2 } {
+ set closed false
if {[chan eof $sock]} {
set dta "Socket closed."
+ set closed true
}
${log}::error "Cannot read header from socket: $dta"
- close $sock 1001
+ if {$closed} {
+ # connection lost can't do close handshake
+ Disconnect $sock
+ } else {
+ close $sock 1001
+ }
return
}
binary scan $dta Su header
@@ -993,6 +1006,9 @@
} else {
close $sock
}
+ # recieved close and sent handshake, now we can close the
+ # socket
+ Disconnect $sock
return
}
9 {
|