{"id":23681403,"url":"https://github.com/codecat/go-enet","last_synced_at":"2025-09-02T10:31:24.560Z","repository":{"id":55689134,"uuid":"284466884","full_name":"codecat/go-enet","owner":"codecat","description":"Enet bindings for Go using cgo.","archived":false,"fork":false,"pushed_at":"2024-12-02T19:02:58.000Z","size":51,"stargazers_count":38,"open_issues_count":0,"forks_count":13,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-02T20:20:04.743Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","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/codecat.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"License.txt","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},"funding":{"github":["codecat"]}},"created_at":"2020-08-02T13:27:37.000Z","updated_at":"2024-12-02T19:03:03.000Z","dependencies_parsed_at":"2024-12-02T20:20:00.798Z","dependency_job_id":"11e4d04a-e2fb-49db-966f-2cd15ece89f1","html_url":"https://github.com/codecat/go-enet","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/codecat%2Fgo-enet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codecat%2Fgo-enet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codecat%2Fgo-enet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codecat%2Fgo-enet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codecat","download_url":"https://codeload.github.com/codecat/go-enet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":231774744,"owners_count":18424669,"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-12-29T18:38:01.965Z","updated_at":"2025-09-02T10:31:24.545Z","avatar_url":"https://github.com/codecat.png","language":"C","funding_links":["https://github.com/sponsors/codecat"],"categories":[],"sub_categories":[],"readme":"# go-enet\nEnet bindings for Go using cgo.\n\n## Installation\nFirst, you might need to install enet as a dependency:\n\n* **Windows**: Nothing to do - should work out of the box with the supplied headers and library.\n* **Linux**: Install the enet development package with your package manager.\n\t* On Debian-based systems: `apt install libenet-dev`\n* **MacOS**: Install the enet package with brew: `brew install enet`\n\nSince this module uses cgo, you must also make sure you have a C compiler in your `PATH`. For Linux and Mac, this is usually pretty straight forward. On Windows, you may have to use something like [MSYS2](https://www.msys2.org/).\n\nWhen ready, get the module like this:\n\n```\n$ go get github.com/codecat/go-enet\n```\n\n## Usage\n```go\nimport \"github.com/codecat/go-enet\"\n```\n\nThe API is mostly the same as the C API, except it's more object-oriented.\n\n## Server example\nThis is a basic server example that responds to packets `\"ping\"` and `\"bye\"`.\n\n```go\npackage main\n\nimport (\n\t\"github.com/codecat/go-enet\"\n\t\"github.com/codecat/go-libs/log\"\n)\n\nfunc main() {\n\t// Initialize enet\n\tenet.Initialize()\n\n\t// Create a host listening on 0.0.0.0:8095\n\thost, err := enet.NewHost(enet.NewListenAddress(8095), 32, 1, 0, 0)\n\tif err != nil {\n\t\tlog.Error(\"Couldn't create host: %s\", err.Error())\n\t\treturn\n\t}\n\n\t// The event loop\n\tfor true {\n\t\t// Wait until the next event\n\t\tev := host.Service(1000)\n\n\t\t// Do nothing if we didn't get any event\n\t\tif ev.GetType() == enet.EventNone {\n\t\t\tcontinue\n\t\t}\n\n\t\tswitch ev.GetType() {\n\t\tcase enet.EventConnect: // A new peer has connected\n\t\t\tlog.Info(\"New peer connected: %s\", ev.GetPeer().GetAddress())\n\n\t\tcase enet.EventDisconnect: // A connected peer has disconnected\n\t\t\tlog.Info(\"Peer disconnected: %s\", ev.GetPeer().GetAddress())\n\n\t\tcase enet.EventReceive: // A peer sent us some data\n\t\t\t// Get the packet\n\t\t\tpacket := ev.GetPacket()\n\n\t\t\t// We must destroy the packet when we're done with it\n\t\t\tdefer packet.Destroy()\n\n\t\t\t// Get the bytes in the packet\n\t\t\tpacketBytes := packet.GetData()\n\n\t\t\t// Respond \"pong\" to \"ping\"\n\t\t\tif string(packetBytes) == \"ping\" {\n\t\t\t\tev.GetPeer().SendString(\"pong\", ev.GetChannelID(), enet.PacketFlagReliable)\n\t\t\t\tcontinue\n\t\t\t}\n\n\t\t\t// Disconnect the peer if they say \"bye\"\n\t\t\tif string(packetBytes) == \"bye\" {\n\t\t\t\tlog.Info(\"Bye!\")\n\t\t\t\tev.GetPeer().Disconnect(0)\n\t\t\t\tcontinue\n\t\t\t}\n\t\t}\n\t}\n\n\t// Destroy the host when we're done with it\n\thost.Destroy()\n\n\t// Uninitialize enet\n\tenet.Deinitialize()\n}\n```\n\n## Client example\nThis is a basic client example that sends a ping to the server every second that there is no event.\n\n```go\npackage main\n\nimport (\n\t\"github.com/codecat/go-enet\"\n\t\"github.com/codecat/go-libs/log\"\n)\n\nfunc main() {\n\t// Initialize enet\n\tenet.Initialize()\n\n\t// Create a client host\n\tclient, err := enet.NewHost(nil, 1, 1, 0, 0)\n\tif err != nil {\n\t\tlog.Error(\"Couldn't create host: %s\", err.Error())\n\t\treturn\n\t}\n\n\t// Connect the client host to the server\n\tpeer, err := client.Connect(enet.NewAddress(\"127.0.0.1\", 8095), 1, 0)\n\tif err != nil {\n\t\tlog.Error(\"Couldn't connect: %s\", err.Error())\n\t\treturn\n\t}\n\n\t// The event loop\n\tfor true {\n\t\t// Wait until the next event\n\t\tev := client.Service(1000)\n\n\t\t// Send a ping if we didn't get any event\n\t\tif ev.GetType() == enet.EventNone {\n\t\t\tpeer.SendString(\"ping\", 0, enet.PacketFlagReliable)\n\t\t\tcontinue\n\t\t}\n\n\t\tswitch ev.GetType() {\n\t\tcase enet.EventConnect: // We connected to the server\n\t\t\tlog.Info(\"Connected to the server!\")\n\n\t\tcase enet.EventDisconnect: // We disconnected from the server\n\t\t\tlog.Info(\"Lost connection to the server!\")\n\n\t\tcase enet.EventReceive: // The server sent us data\n\t\t\tpacket := ev.GetPacket()\n\t\t\tlog.Info(\"Received %d bytes from server\", len(packet.GetData()))\n\t\t\tpacket.Destroy()\n\t\t}\n\t}\n\n\t// Destroy the host when we're done with it\n\tclient.Destroy()\n\n\t// Uninitialize enet\n\tenet.Deinitialize()\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodecat%2Fgo-enet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodecat%2Fgo-enet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodecat%2Fgo-enet/lists"}