{"id":19966657,"url":"https://github.com/engineersmith/enet-example","last_synced_at":"2026-06-05T01:31:56.776Z","repository":{"id":116642152,"uuid":"443612720","full_name":"EngineerSmith/enet-example","owner":"EngineerSmith","description":"Love2D ENet example of a chat program using server-client architecture. ","archived":false,"fork":false,"pushed_at":"2022-06-07T13:36:22.000Z","size":23,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-26T07:59:25.216Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/EngineerSmith.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-01-01T19:52:42.000Z","updated_at":"2023-12-01T16:07:07.000Z","dependencies_parsed_at":null,"dependency_job_id":"f6f10cfc-d0aa-4a49-9a57-c2b3e7d30b39","html_url":"https://github.com/EngineerSmith/enet-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EngineerSmith%2Fenet-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EngineerSmith%2Fenet-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EngineerSmith%2Fenet-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EngineerSmith%2Fenet-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EngineerSmith","download_url":"https://codeload.github.com/EngineerSmith/enet-example/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241398854,"owners_count":19956809,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T02:37:29.932Z","updated_at":"2025-03-01T17:26:14.508Z","avatar_url":"https://github.com/EngineerSmith.png","language":"Lua","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Love2D ENet code example : Client-Server chat\nThe code shows off a simple implementation using ENet of a chat using the client-server architecture model. This code doesn't show off multithreaded to keep the focus simple and on enet. You can learn more about multithreading from [love's wiki](https://love2d.org/wiki/love.thread).\n\nThe code includes comments - but this document goes into a more high level overview of the code.\n# Getting it running\nYou can run either of the bat files or the shell scripts provided. Run the server before starting any clients. Otherwise you can run any of the `main.lua` within their self-containing folders. \n\n`main.lua` on the top level is used as a basic switch between client and server to have a single entry point for the code base. Using the argument `-server`, see the bat or shell files to see this in action.\n# Server\n**NOTE**, this code can be access through the network (E.g. LAN/WAN (web)) if port `12345` is open. You can change this behaviour to run **only** on localhost if you change the address variable within the server's `main.lua` to `localhost` or `127.0.0.1` from the original value of `*`\n## How does the sever work?\nThe server is where the magic of networking starts. First with ENet you must create a [host](https://love2d.org/wiki/enet.host).\n```lua\nlocal host = enet.host_create(address..\":\"..port)\n``` \nThe above is how the address shall be formatted. The address can be of 4 values.\n`*` or `0.0.0.0` allows the server to run open to the network\n`localhost` or `127.0.0.1` allows the server to only run on the machine - not accessible from outside that machine. (Do note that localhost's value can be changed on the machine - by default it will direct to 127.0.0.1)\n\nNow that you've created the host, you need to start the server. To start the server you need to run the `:service()` function. If the server starts it will return an event, otherwise it will not return an event. This usually means the port has been taken by another program - and ports cannot be shared.\n\nLastly we get to the main loop of the server.\n```lua\nlocal event = host:service(50) -- 50ms timeout\nwhile event do\n  log(\"Event received. Type: \"..event.type..\", from:\"..tostring(event.client)..\" containing: \"..tostring(event.data))\n  event = host:check_events() -- get next event in queue\nend -- will exit once the event queue has been emptied\n```\nThere are 3 types of events, the table below contains what variables are available and their type. You can learn more from [love own wiki](https://love2d.org/wiki/enet.event).\n| event.type | event.peer | event.data |\n|--|--|--|\n| \"receive\" | peer | string |\n| \"disconnect\" | peer | number |\n| \"connect\" | peer | number |\n\nMessages are sent between server to client using peer objects that can be found within events from the `event.peer` variable. Peers have many more functions that you access that can be read about on [love's wiki](https://love2d.org/wiki/enet.peer). **NOTE**, to find a peer's connected ip and port you have to use `tostring(peer)` - it isn't documented on love's wiki.\n# Client\nThe client contains about the same amount of code as the server does - it has a few extra functions to capture and send messages however.\n\n## How does the client work?\nIt works almost the same as the server, except it doesn't ask for a variable when creating the host - and requires you to call the connect function on the host. Similarly, it doesn't connect to the server until `:service()` is called\n```lua\nlocal host = enet.host_create()\nlocal server = host:connect(address..\":\"..port) -- server is peer object\nlocal event = host:service(5000) -- connection messages sent here\n```\nDo note that the address to the server will depend which network it is on compared to the client machine.  If running on the same machine then `localhost` will work even if the server uses either `*` or `0.0.0.0`. If the machine is connected on the a different router then you would need to use the public IP, and have port forwarding(NAT) involved on the server's router. If you're running on the same router network then you can use the private IP - but you can use the public IP if the port has been port forwarded (NAT). Sometimes a router is old, and cannot connect to machines within it's own network - then you would need to use public IP with port forwarding. Check out [Cannot connect to server](#Cannot-connect-to-server) if you're having connection issues.\n\nOnce the client has connected to the server the network loop is practically the same as the server's. However, to send a message to the server you have to use the server's peer object created when running the connect function.\n\nIn the client example you can see the chat program sending the message within the `love.keypressed` call-back function when `return` is pressed.\n\n# Cannot connect to server?\nIf you cannot connect to the machine - but can ping it using the ping command in a terminal using the same ip address. Then usually the issue lies with the client or server's firewall blocking the connection. Ensure that love.exe has access through the firewall (If you fuse your game, then that program must also be let through). \n\nIf you're using a private IP that worked one day but doesn't anymore - check that machine still has the same one as router usually refresh the connected devices IPs every 24 hours or so by default.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fengineersmith%2Fenet-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fengineersmith%2Fenet-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fengineersmith%2Fenet-example/lists"}