Josh Posted June 5, 2017 Share Posted June 5, 2017 This simple example creates a server and client on the same machine and sends messages back and forth. Have fun! chat.zip 1 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
reepblue Posted June 5, 2017 Share Posted June 5, 2017 Pretty neat!. Also acts like a nice GUI demo too. Quote Cyclone - Ultra Game System - Component Preprocessor - Tex2TGA - Darkness Awaits Template (Leadwerks) If you like my work, consider supporting me on Patreon! Link to comment Share on other sites More sharing options...
Berken Posted June 5, 2017 Share Posted June 5, 2017 Looks great.. Quote Link to comment Share on other sites More sharing options...
DooMAGE Posted June 5, 2017 Share Posted June 5, 2017 So nice to see client-server stuff working on Leadwerks Quote My Leadwerks games! https://ragingmages.itch.io/ Link to comment Share on other sites More sharing options...
Josh Posted June 5, 2017 Author Share Posted June 5, 2017 If you look in the documentation, it's all finished, sans examples: Client Server Message Peer RemoteGame 1 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
gamecreator Posted June 5, 2017 Share Posted June 5, 2017 5 minutes ago, DoomSlayer said: So nice to see client-server stuff working on Leadwerks I assume you mean with Lua. It's worked with C++ for years. Quote Link to comment Share on other sites More sharing options...
DooMAGE Posted June 5, 2017 Share Posted June 5, 2017 The first thing I'll try to do is enable some sort of ghost multiplayer on Log Riders like the Trackmania series. No player interaction, just you be able to see the other player jumping around. It's doable? Quote My Leadwerks games! https://ragingmages.itch.io/ Link to comment Share on other sites More sharing options...
Josh Posted June 5, 2017 Author Share Posted June 5, 2017 41 minutes ago, DoomSlayer said: The first thing I'll try to do is enable some sort of ghost multiplayer on Log Riders like the Trackmania series. No player interaction, just you be able to see the other player jumping around. It's doable? Yes. The commands are all documented. Use Server:Publish to make your server public, then the clients call CountServers() and GetServer() to get a RemoteGame object. the RemoteGame object has a member for the IP address and the server description, and you use that IP address to connect to the server. Viola. Your server would then just send out messages with the player and log positions / rotations and the client can watch the action. 1 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Einlander Posted June 5, 2017 Share Posted June 5, 2017 I would suggest that you have for the server:publish API you have a mandatory key string parameter. This way you can separate games of the same name and even the same game but different versions. 1 Quote Link to comment Share on other sites More sharing options...
Josh Posted June 6, 2017 Author Share Posted June 6, 2017 4 hours ago, gamecreator said: I assume you mean with Lua. It's worked with C++ for years. The missing piece was really the server list. Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
DooMAGE Posted June 6, 2017 Share Posted June 6, 2017 17 hours ago, gamecreator said: I assume you mean with Lua. It's worked with C++ for years. Yes you are right, I mean native support @Josh Thanks for the instructions! Quote My Leadwerks games! https://ragingmages.itch.io/ Link to comment Share on other sites More sharing options...
Josh Posted June 13, 2017 Author Share Posted June 13, 2017 I'm going to post my WIP part 2 here. This won't work when the server is behind a router. -------------------------------------------------------- --Function to build user interface on each window -------------------------------------------------------- function BuildGUI(context) local interface = {} interface.gui=GUI:Create(context) interface.root = interface.gui:GetBase() interface.root:SetScript("Scripts/GUI/Panel.lua") interface.chatpanel=Widget:Panel(0,0,interface.root:GetSize().x,interface.root:GetSize().y,interface.root) interface.chatpanel:SetAlignment(1,1,1,1) interface.chatlog = Widget:TextArea(4,4,interface.chatpanel:GetClientSize().x-8,interface.chatpanel:GetClientSize().y-30-8,interface.chatpanel) interface.textbox = Widget:TextField("",4,interface.chatpanel:GetClientSize().y-30,interface.chatpanel:GetClientSize().x-8-72-4,26,interface.chatpanel) interface.sendbutton = Widget:Button("Send",interface.chatpanel:GetClientSize().x-4-72,interface.chatpanel:GetClientSize().y-30,72,26,interface.chatpanel) interface.chatpanel:Hide() interface.startpanel=Widget:Panel(0,0,interface.root:GetSize().x,interface.root:GetSize().y,interface.root) interface.clientbutton = Widget:Button("Client",20,20,72,26,interface.startpanel) interface.serverbutton = Widget:Button("Server",20,60,72,26,interface.startpanel) interface.serverpanel=Widget:Panel(0,0,interface.root:GetSize().x,interface.root:GetSize().y,interface.root) interface.serverlist = Widget:ListBox(4,4,interface.serverpanel:GetClientSize().x-8,interface.serverpanel:GetClientSize().y-8-34,interface.serverpanel) interface.serverlist:SetAlignment(1,1,1,1) interface.joinbutton = Widget:Button("Join",4,interface.serverpanel:GetClientSize().y-28-8,interface.serverpanel:GetClientSize().x-4-4,28,interface.serverpanel) interface.serverpanel:Hide() return interface end -------------------------------------------------------- --Initialize client and server -------------------------------------------------------- local MESSAGE_CHAT=1 local peer = nil local port=3074 local appname = "LeadwerksChat62" local connected=false local lastrefreshtime = Time:Millisecs() -------------------------------------------------------- --Create server interface -------------------------------------------------------- window=Window:Create("Chat Server",0,0,400,300,Window.Titlebar) --servercontext=Context:Create(serverwindow) local interface = BuildGUI(window) -------------------------------------------------------- --Main loop -------------------------------------------------------- while true do -------------------------------------------------------- --Exit the program -------------------------------------------------------- if window:KeyHit(Key.Escape) then return end -------------------------------------------------------- --Handle closed windows -------------------------------------------------------- if window:GetHidden()==false then if window:Closed() then if networknode:GetClassName()=="Server" then networknode:Remove() else networknode:Disconnect() end return end end -------------------------------------------------------- --Update -------------------------------------------------------- if connected then while true do if networknode:GetClassName()=="Server" then if (Time:Millisecs()-lastrefreshtime>1000*5) then networknode:Refresh() System:Print("Refresh") lastrefreshtime = Time:Millisecs() end end local message = networknode:Update() if message==nil then break end if message.id==Message.Connect then interface.chatlog:AddText(">> Connected to other computer.") elseif message.id==Message.Disconnect then interface.chatlog:AddText(">> Disconnected from other computer.") elseif message.id==MESSAGE_CHAT then interface.chatlog:AddText("Peer: "..message.stream:ReadString()) end message:Release() end end -------------------------------------------------------- --Handle GUI events -------------------------------------------------------- while EventQueue:Peek() do local event = EventQueue:Wait() if event.id == Event.WidgetAction then if event.source == interface.sendbutton or event.source==interface.textbox then local s = interface.textbox:GetText() if networknode:GetClassName()=="Server" then networknode:Broadcast(MESSAGE_CHAT,s) else networknode:Send(MESSAGE_CHAT,s) end interface.chatlog:AddText("Me: "..s) interface.textbox:SetText("") interface.gui:SetFocus(interface.textbox) elseif event.source == interface.clientbutton then networknode = Client:Create() interface.serverpanel:Show() interface.startpanel:Hide() for n=0,networknode:CountServers(appname)-1 do local remotegame = networknode:GetServer(n) local address = remotegame.address interface.serverlist:AddItem(address,n==0) end elseif event.source == interface.joinbutton then local item = interface.serverlist:GetSelectedItem() if item>-1 then local address = interface.serverlist:GetItemText(item) if networknode:Connect(address,port)==false then System:Print("Failed to join server") return end interface.chatpanel:Show() interface.serverpanel:Hide() connected = true end elseif event.source == interface.serverbutton then networknode = Server:Create(port) networknode:Remove() if networknode:Publish(appname)==false then System:Print("Error: Failed to publish server.") return end interface.chatpanel:Show() interface.startpanel:Hide() connected=true end end end -------------------------------------------------------- --Refresh both contexts -------------------------------------------------------- --context:Sync() end 1 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Christian Clavet Posted July 10, 2017 Share Posted July 10, 2017 It should still work if you forward the 3074 port to your server no? Do you need to forward more ports? Quote Link to comment Share on other sites More sharing options...
GorzenDev Posted July 12, 2017 Share Posted July 12, 2017 very nice i am trying to recreate your example for my own purposes and get stuck at Server:Create() it always returns a nil value for me . at some point i even added a line of code like this what gave the same result if server == nil then server = Server:Create(port) end my Main.lua gives an "attempt to index local 'server' (a nil value)" error i know what this means but cant realy understand why. anybody able to help me out here ? Main.lua --Set the application title title="GameServer" -------------------------------------------------------- --Function to build user interfaca -------------------------------------------------------- function BuildServerGUI(context) local interface = {} interface.gui=GUI:Create(context) interface.root = interface.gui:GetBase() interface.root:SetScript("Scripts/GUI/Panel.lua") interface.chatlog = Widget:TextArea(4,4,interface.root:GetClientSize().x-8,interface.root:GetClientSize().y-30-8,interface.root) interface.textbox = Widget:TextField("",4,interface.root:GetClientSize().y-30,interface.root:GetClientSize().x-8-72-4,26,interface.root) interface.sendbutton = Widget:Button("Send",interface.root:GetClientSize().x-4-72,interface.root:GetClientSize().y-30,72,26,interface.root) return interface end -------------------------------------------------------- --Initialize client and server -------------------------------------------------------- local MESSAGE_CHAT=1 local peer = nil local port=8000 --local client = Client:Create() local server = Server:Create(port) --local success = client:Connect("127.0.0.1",port) -------------------------------------------------------- --Create a server window -------------------------------------------------------- serverwindow=Window:Create(title,0,0,800,600,Window.Titlebar) local serverInterface = BuildServerGUI(serverwindow) -------------------------------------------------------- --Main loop -------------------------------------------------------- while true do -------------------------------------------------------- --Exit the program -------------------------------------------------------- if serverwindow:KeyHit(Key.Escape) then return end -------------------------------------------------------- --Handle closed window -------------------------------------------------------- if serverwindow:GetHidden()==false then if serverwindow:Closed() then serverwindow:Hide() server:Disconnect(peer) end end -------------------------------------------------------- --Update server -------------------------------------------------------- while true do if server == nil then server = Server:Create(port) end local message = server:Update() <<----------Gives error on this line if message==nil then break end if message.id==Message.Connect then serverInterface.chatlog:AddText(">> New client connected.") peer = message.peer elseif message.id==Message.Disconnect then serverInterface.chatlog:AddText(">> Client disconnected.") elseif message.id==MESSAGE_CHAT then serverInterface.chatlog:AddText("Client: "..message.stream:ReadString()) end message:Release() end -------------------------------------------------------- --Handle GUI events -------------------------------------------------------- while EventQueue:Peek() do local event = EventQueue:Wait() if event.id == Event.WidgetAction then if event.source == serverInterface.sendbutton or event.source==serverInterface.textbox then local s = serverInterface.textbox:GetText() server:Send(peer,MESSAGE_CHAT,s) serverInterface.chatlog:AddText("Me: "..s) serverInterface.textbox:SetText("") serverInterface.gui:SetFocus(serverInterface.textbox) end end end end Quote Link to comment Share on other sites More sharing options...
Josh Posted July 12, 2017 Author Share Posted July 12, 2017 Change the port number? 1 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
GorzenDev Posted July 12, 2017 Share Posted July 12, 2017 19 minutes ago, Josh said: Change the port number? this did work thank you for that. could you maiby explain why ? my expertise in networking is limited Quote Link to comment Share on other sites More sharing options...
Josh Posted July 12, 2017 Author Share Posted July 12, 2017 11 minutes ago, GorzenDev said: this did work thank you for that. could you maiby explain why ? my expertise in networking is limited Your network card does not have that port? Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
GorzenDev Posted July 12, 2017 Share Posted July 12, 2017 18 minutes ago, Josh said: Your network card does not have that port? like i said my expertise in networking is limited so there is no way i can say you are wrong. but i have ran gameservers for steamgames and those are in the port ranges of 27000. anyway thanks for the help i can tinker some more now (fun fun ) Quote Link to comment Share on other sites More sharing options...
Einlander Posted July 12, 2017 Share Posted July 12, 2017 Avoid using port 8000 and 8080, those are standard web ports. Around 20000 or so it is safe to use. 1 Quote Link to comment Share on other sites More sharing options...
GorzenDev Posted July 13, 2017 Share Posted July 13, 2017 2 hours ago, Einlander said: Avoid using port 8000 and 8080, those are standard web ports. Around 20000 or so it is safe to use. makes sence if the port is in use the Server:Create() returns nil Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.