I have been working with AutoLan on and off for some time now. It’s working great for me and I am really impressed.
But now I am stuck switching the client on and off.
I have a function that should handle the closing of a client. When I try to reconnect again, I will have two servers with the same IP, retry that and there will be three and so on…
xnailbender and others have aced this, but I can’t. Reading through the thread there may have been some enhancements to the code. I am working with the 1.20 version.
Any thoughts on what I am doing wrong? Thanks for your replies.
This is my code to connect the client:
function makeClient()
if(isServer) then --if we were a server before, we need to unregister all the event listeners
Runtime:removeEventListener("autolanPlayerDropped", playerDropped)
Runtime:removeEventListener("autolanPlayerJoined", addPlayer)
Runtime:removeEventListener("autolanReceived", serverReceived) --all incoming packets sent to serverReceived
Runtime:removeEventListener("autolanFileReceived", autolanFileReceived)
serverList = {}
end
print("making client")
isLocal = false
isClient = true
isServer = false
clientList = {}
if client == nil then
client = require("Client")
end
client:start()
client:scanServers()
Runtime:addEventListener("autolanConnected", autolanConnected)
Runtime:addEventListener("autolanServerFound", autolanServerFound)
Runtime:addEventListener("autolanDisconnected", autolanDisconnected)
Runtime:addEventListener("autolanReceived", autolanReceived)
Runtime:addEventListener("autolanConnectionFailed", autolanConnectionFailed)
end
and this is my server code:
function makeServer()
if(isClient) then --if we were a client before, we need to unregister all the event listeners
isClient = false
Runtime:removeEventListener("autolanConnected", clientReceived) --all incoming packets are sent to clientReceived
Runtime:removeEventListener("autolanServerFound", connectionAttemptFailed)
Runtime:removeEventListener("autolanDisconnected", connectionAttemptFailed)
Runtime:removeEventListener("autolanReceived", connectionAttemptFailed)
Runtime:removeEventListener("autolanConnectionFailed", connectionAttemptFailed)
client:disconnect()
client:stop()
end
print("making server")
isLocal = false
isServer = true
isClient = false
serverList = {}
if server == nil then
server = require("Server")
end
server:setCustomBroadcast({"Players", defMaxNoOfClients})
server:startLocal()
Runtime:addEventListener("autolanPlayerJoined", autolanPlayerJoined)
Runtime:addEventListener("autolanPlayerDropped", autolanPlayerDropped)
Runtime:addEventListener("autolanReceived", autolanServerReceived)
end
This is the code to cancel the server or client, due to a timeout or request from the user:
function killServerClient()
print("killServerClient")
if isLocal == false then
if(isClient) then --if we were a client before, we need to unregister all the event listeners
print("Client Off")
Runtime:removeEventListener("autolanConnected", clientReceived) --all incoming packets are sent to clientReceived
Runtime:removeEventListener("autolanServerFound", connectionAttemptFailed)
Runtime:removeEventListener("autolanDisconnected", connectionAttemptFailed)
Runtime:removeEventListener("autolanReceived", connectionAttemptFailed)
Runtime:removeEventListener("autolanConnectionFailed", connectionAttemptFailed)
client:disconnect()
client:stop()
end
if(isServer) then --if we were a server before, we need to unregister all the event listeners
print("Server Off")
Runtime:removeEventListener("autolanPlayerDropped", playerDropped)
Runtime:removeEventListener("autolanPlayerJoined", addPlayer)
Runtime:removeEventListener("autolanReceived", serverReceived) --all incoming packets sent to serverReceived
timerActive = false
server:disconnect()
server:stop()
end
end
clientList = {}
serverList = {}
isLocal = true
isServer = false
isClient = false
storyboard.gotoScene( "mainScene", "fromLeft", 100 )
end
Thanks
Holger [import]uid: 88712 topic_id: 19917 reply_id: 109275[/import]