Logic Machine Forum
websocket library help - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: websocket library help (/showthread.php?tid=1815)



websocket library help - rocfusion - 04.01.2019

Hi,

Referring to post Websocket post we are able to connect to websocket.  Now when I try to connect to a secure websocket using copas and the url includes a query string then the query string is dropped. for example.  How can we solve this?

Code:
if not skt then
 --add receive thread
 copas.addthread(function()
     local reconnect = function()
       local url= "wss://192.168.0.54:8002/api/v2/channels/remote.control?name=MTUyNjQ1MTQ="
       skt,err = ws.client('copas',0)
       local noerr,a = skt:connect(url)

       if(noerr==true) then        
         init()
       else
         alert('[tcp-client] error connecting %s',a)
         return
       end      
     end
     if not skt or skt==nil or skt.state=='CLOSED' then
       reconnect()        
     end
     if(skt) then
       while true do
         local resp,err = copas.receive(skt)
         -- if theres no connection start a new connection
         if(skt.state=='CLOSED')then
           if not resp then
             alert("[tcp-client] Receive error: %s", err)
             copas.removeserver(skt)
             skt = nil
             break
           end
         else
           local fd,prtd = pcall(parse,resp)
           if(fd==false)then
             alert("Error with parsemsg %s ",prtd)
           end
         end
       end      
     end
   end)  
end



RE: websocket library help - admin - 04.01.2019

Replace upgrade_request function:
Code:
local upgrade_request = function(req, key)
  local format = string.format
  local uri = req.path or ''
  local query = req.query
  if type(query) == 'string' and #query > 0 then
    uri = uri .. '?' .. query
  end

  local lines = {
    format('GET %s HTTP/1.1', uri),
    format('Host: %s', req.host),
    'Upgrade: websocket',
    'Connection: Upgrade',
    format('Sec-WebSocket-Key: %s',key),
    'Sec-WebSocket-Version: 13',
  }
  if req.port and req.port ~= 80 then
    lines[2] = format('Host: %s:%d', req.host, req.port)
  end
  if req.userinfo then
    local auth = format('Authorization: Basic %s', base64enc(req.userinfo))
    tinsert(lines, auth)
  end
  tinsert(lines,'\r\n')
  return tconcat(lines,'\r\n')
end