Skip to content

Commit

Permalink
Merge 53c5fcb into 12c593a
Browse files Browse the repository at this point in the history
  • Loading branch information
NovusTheory authored Jun 12, 2016
2 parents 12c593a + 53c5fcb commit c7c97d8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
35 changes: 22 additions & 13 deletions src/websocket/client_ev.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

local socket = require'socket'
local tools = require'websocket.tools'
local frame = require'websocket.frame'
local handshake = require'websocket.handshake'
local debug = require'debug'
local tconcat = table.concat
local tinsert = table.insert
local ssl = require'ssl'

local ev = function(ws)
ws = ws or {}
Expand Down Expand Up @@ -44,7 +44,7 @@ local ev = function(ws)
message_io = nil
end
if sock then
sock:shutdown()
--sock:shutdown() Doesn't work with secure connections
sock:close()
sock = nil
end
Expand Down Expand Up @@ -74,7 +74,9 @@ local ev = function(ws)
end
end
local handle_socket_err = function(err,io,sock)
if self.state == 'OPEN' then
if err == "wantread" or err == "wantwrite" then
-- Leave it alone because Secure Connection is trying to establish
elseif self.state == 'OPEN' then
on_close(false,1006,err)
elseif self.state ~= 'CLOSED' then
on_error(err)
Expand Down Expand Up @@ -106,13 +108,28 @@ local ev = function(ws)
async_send(encoded, nil, handle_socket_err)
end

self.connect = function(_,url,ws_protocol)
self.connect = function(_,url,ws_protocol,secure_params)
if self.state ~= 'CLOSED' then
on_error('wrong state',true)
return
end
-- Preconnect for SSL if needed
assert(not sock)
sock = socket.tcp()
fd = sock:getfd()
assert(fd > -1)
-- set non blocking
sock:settimeout(0)
sock:setoption('tcp-nodelay',true)
local protocol,host,port,uri = tools.parse_url(url)
if protocol ~= 'ws' then
local connected,err = sock:connect(host,port)
if protocol == 'wss' then
sock = ssl.wrap(sock, secure_params)
sock:dohandshake()

-- We have to re-initialize values that were set previously because it's a new socket
sock:settimeout(0)
elseif protocol ~= "ws" then
on_error('bad protocol')
return
end
Expand All @@ -123,13 +140,6 @@ local ev = function(ws)
ws_protocols_tbl = ws_protocol
end
self.state = 'CONNECTING'
assert(not sock)
sock = socket.tcp()
fd = sock:getfd()
assert(fd > -1)
-- set non blocking
sock:settimeout(0)
sock:setoption('tcp-nodelay',true)
async_send,send_io_stop = require'websocket.ev_common'.async_send(sock,loop)
handshake_io = ev.IO.new(
function(loop,connect_io)
Expand Down Expand Up @@ -190,7 +200,6 @@ local ev = function(ws)
end,
handle_socket_err)
end,fd,ev.WRITE)
local connected,err = sock:connect(host,port)
if connected then
handshake_io:callback()(loop,handshake_io)
elseif err == 'timeout' or err == 'Operation already in progress' then
Expand Down
2 changes: 1 addition & 1 deletion src/websocket/client_sync.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ local new = function(ws)
end

self.sock_close = function(self)
--self.sock:shutdown() Causes errors?
--self.sock:shutdown() Doesn't work with secure connections
self.sock:close()
end

Expand Down
5 changes: 5 additions & 0 deletions src/websocket/ev_common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ local message_io = function(sock,loop,on_message,on_error)
if err then
if err == 'timeout' and #part == 0 then
return
elseif err == 'wantread' and #part == 0 then
return
elseif err == 'wantwrite' and #part == 0 then
return
elseif #part == 0 then
if message_io then
message_io:stop(loop)
Expand All @@ -128,6 +132,7 @@ local message_io = function(sock,loop,on_message,on_error)
encoded = encoded or part
end


repeat
local decoded,fin,opcode,rest = frame.decode(encoded)
if decoded then
Expand Down
4 changes: 2 additions & 2 deletions src/websocket/sync.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ local close = function(self,code,reason)
return was_clean,code,reason or ''
end

local connect = function(self,ws_url,ws_protocol,ssl_params)
local connect = function(self,ws_url,ws_protocol,secure_params)
if self.state ~= 'CLOSED' then
return nil,'wrong state'
end
Expand All @@ -128,7 +128,7 @@ local connect = function(self,ws_url,ws_protocol,ssl_params)
return nil,err
end
if protocol == 'wss' then
self.sock = ssl.wrap(self.sock, ssl_params)
self.sock = ssl.wrap(self.sock, secure_params)
self.sock:dohandshake()
elseif protocol ~= "ws" then
return nil, 'bad protocol'
Expand Down

0 comments on commit c7c97d8

Please sign in to comment.