Code:
function dehex(str)
str = string.gsub(str,'[^%a-zA-Z0-9]','')
return (string.gsub(str, '..', function (cc) return string.char(tonumber(cc, 16)) end))
end
function fixkey(str)
local num = ""
string.gsub(str,'%d', function(d) num = num .. d end)
local _,n = string.gsub(str, ' ', ' ')
local i = tonumber(num)/n
return string.char(math.mod(i/256^3,256), math.mod(i/256^2,256), math.mod(i/256,256),math.mod(i,256))
end
function websocketresponse(key1, key2, end8)
require('bit')
local cat = fixkey(key1) .. fixkey(key2) .. end8
local md5 = bit.newMD5()
md5:update(cat)
return dehex(md5:final())
end
function WScallback(p1,p2)
if ( p2 == transport.constants.event.CONNECTIONCLOSED ) then
print("WEBSOCKET Connection Closed by Girder")
return
end
if ( p2 == transport.constants.event.NEWCONNECTION ) then
print("WEBSOCKET New Connection")
WSclients[p1]=true
--actually need two different terminators \r\n for header and string.char(255) once the websocket is open...
p1:Callback(transport.constants.parser.TERMINATED,string.char(255), 2000, function (cp1,cp2)
if ( cp2 == transport.constants.event.CONNECTIONCLOSED ) then
print("WEBSOCKET Connection Closed by client (can be the response to Girder sending closing message)")
p1:Close()
WSclients[p1]=nil
elseif (cp2 == transport.constants.event.INCOMPLETERESPONSETIMEOUT) then
local connecting = string.Split(string.gsub(cp1,'\r',''),"\n")
local response = 'HTTP/1.1 101 WebSocket Protocol Handshake\r\n'
response = response .. 'Upgrade: WebSocket\r\n'
response = response .. 'Connection: Upgrade\r\n'
--response = response .. 'Sec-WebSocket-Protocol: chat\r\n'
for i in connecting do
if(string.sub(connecting[i],1,6) == 'Host: ') then
response = response .. 'Sec-WebSocket-Location: ws://'..string.sub(connecting[i],7)..'/\r\n'
elseif(string.sub(connecting[i],1,8) == 'Origin: ') then
response = response .. 'Sec-WebSocket-'..connecting[i]..'\r\n'
elseif (string.find(connecting[i],'Key1') ~= nil) then
websocket_key1 = string.sub(connecting[i],string.find(connecting[i],':',1)+2)
elseif (string.find(connecting[i],'Key2') ~= nil) then
websocket_key2 = string.sub(connecting[i],string.find(connecting[i],':',1)+2)
elseif (string.len(connecting[i]) == 8) then
ending8 = connecting[i]
end
end
p1:Write(response)
p1:Write('\r\n')
p1:Write(websocketresponse(websocket_key1, websocket_key2, ending8))
print('WEBSOCKET Client Connected.')
elseif (cp2 == transport.constants.event.RXCHAR) then
--need to save a client's string
print('WEBSOCKET Client Sent: ' .. string.sub(cp1,2))
end
end)
return
end
end
if ( WSclients ) then
for c,v in pairs(WSclients) do
c:Close()
end
end
if ( WSt ) then
WSt:Close()
win.Sleep(1000)
end
WSclients = {}
WSt = transport.New(transport.constants.transport.GIPLISTEN)
if not WSt:Open(nil, 8080) then
print("Could not create WEBSOCKET Server.")
WSt:Close();
WSt = nil;
end
WSt:Callback(transport.constants.parser.TERMINATED, '\r\n', 2000, WScallback)
print("Starting WEBSOCKET Server...")
--gir.TriggerEvent("WS_broadcast", "100", 0)
In this code? Looks like you are already doing that reference?