{"id":15722592,"url":"https://github.com/britzl/defnet","last_synced_at":"2025-06-21T22:40:16.138Z","repository":{"id":11083314,"uuid":"68282660","full_name":"britzl/defnet","owner":"britzl","description":"Defold networking examples","archived":false,"fork":false,"pushed_at":"2024-07-29T11:11:31.000Z","size":2554,"stargazers_count":70,"open_issues_count":2,"forks_count":16,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-01-21T16:09:57.716Z","etag":null,"topics":["defold","defold-library","lua","luasocket","networking"],"latest_commit_sha":null,"homepage":"","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/britzl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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":"2016-09-15T09:44:59.000Z","updated_at":"2025-01-15T22:53:01.000Z","dependencies_parsed_at":"2024-07-21T21:16:53.352Z","dependency_job_id":"9c4838fa-aef8-4de6-92f8-a1a2d55e9711","html_url":"https://github.com/britzl/defnet","commit_stats":null,"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/britzl%2Fdefnet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/britzl%2Fdefnet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/britzl%2Fdefnet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/britzl%2Fdefnet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/britzl","download_url":"https://codeload.github.com/britzl/defnet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":236176769,"owners_count":19107393,"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":["defold","defold-library","lua","luasocket","networking"],"created_at":"2024-10-03T22:08:36.589Z","updated_at":"2025-01-29T12:10:10.064Z","avatar_url":"https://github.com/britzl.png","language":"Lua","readme":"# DefNet\nDefold Networking modules and examples, provided as a Defold library project.\n\n## Preface\nI often get questions about networking in Defold from our forum users. Sometimes it is as easy as \"How do I make an HTTP call?\" and I can point to the documentation for [http.request()](http://www.defold.com/ref/http/#http.request:url-method-callback--headers---post_data---options-) and everyone is happy. Other times the questions are more complex (often relating to socket connections) and in those cases the Defold documentation isn't enough.\n\nLuckily for us Defold comes bundles with the excellent [LuaSocket](http://w3.impa.br/~diego/software/luasocket/home.html) library. With LuaSocket it is fairly trivial to create TCP and UDP sockets. Here's a bare-bones TCP socket example:\n\n```\nlocal client = socket.tcp()\nclient:connect(server_ip, server_port)\nclient:settimeout(0) -- non blocking socket\nclient:send(data) -- send data like this\nlocal response = client:receive(\"*l\") -- receive a \"line\" of data like this\n```\n\nThe above snippet of code, some reading of the LuaSocket documentation and perhaps a couple of Google searches will get you quite far, but some concepts like peer to peer discovery is a bit trickier. The goal with this project is to collect some useful Lua networking modules that can be used either as-is or modified to suit your needs.\n\n## Requirements\n\n\n## Installation\nYou can use the modules from this project in your own project by adding this project as a [Defold library dependency](http://www.defold.com/manuals/libraries/). Open your game.project file and in the `dependencies` field under `project` add:\n\n\thttps://github.com/britzl/defnet/archive/master.zip\n\nOr point to the ZIP file of a [specific release](https://github.com/britzl/defnet/releases).\n\n## Peer to peer discovery\nThe `defnet/p2p_discovery` module can be used to perform peer to peer discovery using UDP sockets. The basic idea is that an app sets itself up as discoverable and starts sending a broadcast message on the network. Other clients can listen for broadcasted messages and get the IP of the app that wishes to be discovered. This is how an app can set itself up as discoverable:\n\n```\nlocal p2p_discovery = require \"defnet.p2p_discovery\"\nlocal PORT = 50000\n\nfunction init(self)\n\tself.p2p = p2p_discovery.create(PORT)\n\tself.p2p.broadcast(\"findme\")\nend\n\nfunction update(self, dt)\n\tself.p2p.update()\nend\n```\n\nAnd this is how another app would discover it:\n\n```\nlocal p2p_discovery = require \"defnet.p2p_discovery\"\nlocal PORT = 50000\n\nfunction init(self)\n\tself.p2p = p2p_discovery.create(PORT)\n\tself.p2p.listen(\"findme\", function(ip, port, message)\n\t\tprint(\"Found server\", ip, port)\n\t\tprint(\"Message\", message)\n\t\tself.p2p.stop()\n\tend)\nend\n\nfunction update(self, dt)\n\tself.p2p.update()\nend\n```\n\nOnce discovery has been completed communication can take place over a socket of some kind.\n\nNOTE: While it may be possible to test with two or more clients running on the same machine it is not always guaranteed to work. It is recommended to test P2P discovery on multiple machines for the most accurate results.\n\n## TCP socket server\nThe `defnet/tcp_server` module can be used to create a TCP socket server that accepts incoming TCP client connections and can send and receive data. Example:\n\n```\nlocal function on_data(data, ip, port, client)\n\tprint(\"Received\", data, \"from\", ip)\n\treturn \"My response\\n\"\nend\n\nlocal function on_client_connected(ip, port, client)\n\tprint(\"Client\", ip, \"connected\")\nend\n\nlocal function on_client_disconnected(ip, port, client)\n\tprint(\"Client\", ip, \"disconnected\")\nend\n\nfunction init(self)\n\tself.server = tcp_server.create(8190, on_data, on_client_connected, on_client_disconnected)\n\tself.server.start()\nend\n\nfunction final(self)\n\tself.server.stop()\nend\n\nfunction update(self, dt)\n\tself.server.update()\nend\n```\n\n\n## TCP socket client\nThe `defnet/tcp_client` module can be used to create a TCP socket client and connect it. Example:\n\n```\nlocal tcp_client = require \"defnet.tcp_client\"\nlocal IP = \"localhost\" -- perhaps get IP from P2P discovery?\nlocal PORT = 9189\n\nfunction init(self)\n\tself.client = tcp_client.create(IP, PORT, function(data)\n\t\tprint(\"TCP client received data \" .. data)\n\tend,\n\tfunction()\n\t\tprint(\"On disconnected\")\n\t\tself.client = nil\n\tend)\nend\n\nfunction update(self, dt)\n\tif self.client then\n\t\tself.client.update()\n\tend\nend\n\nfunction on_input(self, action_id, action)\n\t-- on some condition do:\n\tself.client.send(\"Sending this to the server\\n\")\nend\n```\n\n\n## UDP client\nThe `defnet/udp` module can be used to create an UDP connection. The connection can either be unconnected and send and receive data from any IP and port or connected and bound to a specific port and/or send only to a specific IP and port.\n\nThe module will simply forward any incoming data to a callback function. Example:\n\n```\nlocal udp = require \"defnet.udp\"\n\nfunction init(self)\n\tself.udp_server = udp.create(function(data, ip, port)\n\t\tprint(\"Received data\", data, ip, port)\n\tend, 9999)\n\n\tself.udp1 = udp.create(function(data, ip, port)\n\t\tprint(\"Received data\", data, ip, port)\n\tend, nil, \"127.0.0.1\", 9999)\n\tself.udp1.send(\"foobar to server\")\n\n\tself.udp2 = udp.create(function(data, ip, port)\n\t\tprint(\"Received data\", data, ip, port)\n\tend)\n\tself.udp2.send(\"foobar to server\", \"127.0.0.1\", 9990)\nend\n\nfunction final(self)\n\tself.udp_server.destroy()\n\tself.udp1.destroy()\n\tself.udp2.destroy()\nend\n\nfunction update(self, dt)\n\tself.udp_server.update()\n\tself.udp1.update()\n\tself.udp2.update()\nend\n```\n\n\n## HTTP server\nSince it's possible to create a TCP socket it's also possible to build more advanced things such as HTTP servers. The `defnet/http_server` module can be used to create a simple HTTP server with basic page routing support. Example:\n\n```\nlocal http_sever = require \"defnet.http_server\"\nlocal PORT = 9189\n\nfunction init(self)\n\tself.hs = http_server.create(PORT)\n\tself.hs.router.get(\"/foo/(.*)$\", function(matches)\n\t\treturn self.http_server.html(\"boo\" .. matches[1])\n\tend)\n\tself.hs.router.get(\"^/$\", function()\n\t\treturn self.http_server.html(\"Hello World\")\n\tend)\n\tself.hs.router.get(\"^/stream$\", function(matches, stream)\n\t\treturn function()\n\t\t\tstream(\"some data\")\n\t\tend\n\tend)\n\tself.hs.router.unhandled(function(method, uri)\n\t\treturn self.http_server.html(\"Oops, couldn't find that one!\", http_server.NOT_FOUND)\n\tend)\n\tself.hs.start()\nend\n\nfunction update(self, dt)\n\tself.hs.update()\nend\n```\n\n\n## WebSockets\nThe WebSocket implementation for Defold has been [moved to a separate project](https://github.com/britzl/defold-websocket).\n\n\n## Example\nThere's a few example projects in the examples folder.\n\n\n## Found a bug? Made an improvement?\nThere are probably bugs. Please report them!\n\nDid you improve some of the code? Please create a Pull Request!\n","funding_links":[],"categories":["Libraries"],"sub_categories":["Programming Language"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbritzl%2Fdefnet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbritzl%2Fdefnet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbritzl%2Fdefnet/lists"}