Skip to content

Commit

Permalink
Merge pull request #52 from yuya373/fix-nowait
Browse files Browse the repository at this point in the history
execute handshake after connection opened
  • Loading branch information
ahyatt committed Apr 3, 2018
2 parents 7d2adf2 + b3a0153 commit e9d148f
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions websocket.el
Original file line number Diff line number Diff line change
Expand Up @@ -728,14 +728,27 @@ to the websocket protocol.
(websocket-outer-filter websocket output))))
(set-process-sentinel
conn
(lambda (process change)
(let ((websocket (process-get process :websocket)))
(websocket-debug websocket "State change to %s" change)
(when (and
(member (process-status process) '(closed failed exit signal))
(not (eq 'closed (websocket-ready-state websocket))))
(websocket-try-callback 'websocket-on-close 'on-close websocket)))))
(websocket-sentinel url conn key protocols extensions custom-header-alist nowait))
(set-process-query-on-exit-flag conn nil)
(unless nowait
(websocket-handshake url conn key protocols extensions custom-header-alist))
websocket))

(defun websocket-sentinel (url conn key protocols extensions custom-header-alist nowait)
#'(lambda (process change)
(let ((websocket (process-get process :websocket)))
(websocket-debug websocket "State change to %s" change)
(let ((status (process-status process)))
(when (and nowait (eq status 'open))
(websocket-handshake url conn key protocols extensions custom-header-alist))

(when (and (member status '(closed failed exit signal))
(not (eq 'closed (websocket-ready-state websocket))))
(websocket-try-callback 'websocket-on-close 'on-close websocket))))))

(defun websocket-handshake (url conn key protocols extensions custom-header-alist)
(let ((url-struct (url-generic-parse-url url))
(websocket (process-get conn :websocket)))
(process-send-string conn
(format "GET %s HTTP/1.1\r\n"
(let ((path (url-filename url-struct)))
Expand All @@ -745,8 +758,7 @@ to the websocket protocol.
(process-send-string conn
(websocket-create-headers
url key protocols extensions custom-header-alist))
(websocket-debug websocket "Websocket opened")
websocket))
(websocket-debug websocket "Websocket opened")))

(defun websocket-process-headers (url headers)
"On opening URL, process the HEADERS sent from the server."
Expand Down

0 comments on commit e9d148f

Please sign in to comment.