I'm writing a chat server/client in LOVE using LUBE and I've run into a frustrating problem. The application worked at first, but after a short while, ceased to work. I have both my client and server set to use port 11988. I decided to do a little investigating to see why it stopped working. Using Wireshark I can see that my LOVE client changed what source port it was using to connect to the server. If I change the port values in my script to this new port, it works again for awhile, but then ceases to work (with the source port changing again.) This will happen again and again.
Screenshot of problem: Server Code:
Code: Select all
require "LUBE.lua"
clientData = {}
-- Server Callbacks
function onConnect(ip, port)
clientData[ip] = {nameSet = nil,
name = "Guest" .. math.random(1,2000)}
print("Client ["..ip.."] connected, assigned name: " .. clientData[ip].name)
end
function onReceive(data, ip, port)
print("Data recieved:" .. data)
-- Parse message
delim = string.find(data,string.char(1))
cmd = string.sub(data,1,delim-1)
msg = string.sub(data,delim+1,#data)
if cmd == "name" then
print(clientData[ip].name.." changed their name to " .. msg)
clientData[ip].name = msg
end
if cmd == "msg" then
server:send("msg"..string.char(1)..clientData[ip].name..": "..msg)
end
end
function onDisconnect(ip, port)
end
function love.load()
server = lube.server(11988)
server:setCallback(onReceive, onConnect, onDisconnect)
server:setHandshake("LoveChat Handshake")
print("LoveChat v0.1 \nby Ninwa\n\n")
end
function love.update(dt)
server:update(dt)
end
function love.draw()
end
Code: Select all
require "LUBE.lua"
function love.load()
client = lube.client()
client:setHandshake("LoveChat Handshake")
client:setCallback(onReceive)
client:connect("24.192.196.87", 11988)
nameEntered = false
name = ""
prevMsgBuffer = {}
msgBuffer = ""
end
function onReceive(data)
-- Parse message
delim = string.find(data,string.char(1))
cmd = string.sub(data,1,delim-1)
msg = string.sub(data,delim+1,#data)
print("data recieved: " .. data)
if cmd == "msg" then
table.insert(prevMsgBuffer,msg)
end
end
function love.update(dt)
client:update(dt)
end
function love.keypressed(key,unicode)
if not nameEntered then
if key == "return" then
if #name > 2 then
client:send("name"..string.char(1)..name)
nameEntered = true
end
return
end
if key == "backspace" then
name = string.sub(name,1,#name-1)
return
end
name = name .. key
return
end
if key == "return" then
if #name > 2 then
client:send("msg"..string.char(1)..msgBuffer)
msgBuffer = ""
end
return
end
if key == "backspace" then
msg = string.sub(msgBuffer,1,#msgBuffer-1)
return
end
msgBuffer = msgBuffer .. key
end
function love.draw()
-- Draw user name prompt
if not nameEntered then
love.graphics.print("Enter your name: " .. name,20,20)
return
end
-- Draw enter message prompt
love.graphics.print("message: "..msgBuffer,40,20)
-- Redraw messages when there's a new one
for i=#prevMsgBuffer, 1, -1 do
love.graphics.print(prevMsgBuffer[i],40,14*(#prevMsgBuffer-i)+50)
end
end
Ninwa