{"id":20439850,"url":"https://github.com/v-braun/nett","last_synced_at":"2026-05-21T16:35:28.045Z","repository":{"id":57610950,"uuid":"165519161","full_name":"v-braun/nett","owner":"v-braun","description":"Go's net.Conn wrapper with simple API","archived":false,"fork":false,"pushed_at":"2019-01-13T16:17:28.000Z","size":98,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-15T20:20:53.505Z","etag":null,"topics":["framework","golang","graceful","network-framework","networking","server","tcp","udp"],"latest_commit_sha":null,"homepage":null,"language":"Go","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/v-braun.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-01-13T15:06:40.000Z","updated_at":"2019-01-13T16:17:29.000Z","dependencies_parsed_at":"2022-09-11T04:50:37.699Z","dependency_job_id":null,"html_url":"https://github.com/v-braun/nett","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/v-braun%2Fnett","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/v-braun%2Fnett/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/v-braun%2Fnett/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/v-braun%2Fnett/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/v-braun","download_url":"https://codeload.github.com/v-braun/nett/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241989183,"owners_count":20053794,"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":["framework","golang","graceful","network-framework","networking","server","tcp","udp"],"created_at":"2024-11-15T09:19:56.125Z","updated_at":"2026-05-21T16:35:23.025Z","avatar_url":"https://github.com/v-braun.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# nett   \r\n\u003e Go's net.Conn wrapper with simple API\r\n\r\nBy [v-braun - viktor-braun.de](https://viktor-braun.de).\r\n\r\n[![](https://img.shields.io/github/license/v-braun/nett.svg?style=flat-square)](https://github.com/v-braun/nett/blob/master/LICENSE)\r\n[![Build Status](https://img.shields.io/travis/v-braun/nett.svg?style=flat-square)](https://travis-ci.org/v-braun/nett)\r\n[![codecov](https://codecov.io/gh/v-braun/nett/branch/master/graph/badge.svg)](https://codecov.io/gh/v-braun/nett)\r\n![PR welcome](https://img.shields.io/badge/PR-welcome-green.svg?style=flat-square)\r\n\r\n\u003cp align=\"center\"\u003e\r\n\u003cimg width=\"70%\" src=\"https://github.com/v-braun/nett/blob/master/.assets/logo.png\" /\u003e\r\n\u003c/p\u003e\r\n\r\n\r\n## Description  \r\n\r\n**nett** *(German word for nice)* is a wrapper for the net.Conn interface  \r\nIt provides a simple event based interface and supports sync and async send operations\r\n\r\n## Installation\r\n```sh\r\ngo get github.com/v-braun/nett\r\n```\r\n\r\n\r\n\r\n## Usage\r\n\r\n``` golang\r\n\r\nvar c1, c2 net.Conn = createConnections() // create your connections\r\nclient1 := nett.Wrap(c1, nett.ReadLineReader)\r\nclient2 := nett.Wrap(c2, nett.ReadLineReader)\r\n\r\nclient1.OnData(func(conn Connection, data []byte) {\r\n    assert.Equal(t, \"ping\\n\", string(data))\r\n    conn.Send([]byte(\"pong\\n\"))\r\n})\r\nclient2.OnData(func(conn Connection, data []byte) {\r\n    assert.Equal(t, \"pong\\n\", string(data))\r\n})\r\n\r\nclient2.SendAsync([]byte(\"ping\\n\"))\r\n\r\n```\r\n\r\nSee also the example in [nett_test.go](https://github.com/v-braun/nett/blob/58d050c19512052eef1daa9020285eb48cdafc1d/nett_test.go#L271):\r\n\r\n```golang\r\nfunc ExamplePingPong() {\r\n\twg := \u0026sync.WaitGroup{}\r\n\r\n\t// create a listener\r\n\tsrvAddr, _ := net.ResolveTCPAddr(\"tcp\", \":0\")\r\n\ts, _ := net.Listen(\"tcp\", srvAddr.String())\r\n\r\n\t// setup async accept for the listener\r\n\tsrvConnChan := make(chan net.Conn)\r\n\tgo func() {\r\n\t\tfor {\r\n\t\t\tc, _ := s.Accept()\r\n\t\t\tif c != nil {\r\n\t\t\t\tsrvConnChan \u003c- c\r\n\t\t\t}\r\n\t\t}\r\n\t}()\r\n\r\n\t// dial to the listener above\r\n\tclntAddr, _ := net.ResolveTCPAddr(\"tcp\", \":0\")\r\n\tc1, _ := net.DialTCP(\"tcp\", clntAddr, s.Addr().(*net.TCPAddr))\r\n\tc2 := \u003c-srvConnChan\r\n\r\n\twg.Add(2) // expect a ping and a pong\r\n\tclient1 := nett.Wrap(c1, nett.ReadLineReader)\r\n\tclient2 := nett.Wrap(c2, nett.ReadLineReader)\r\n\r\n\tclient1.OnData(func(c nett.Connection, data []byte) {\r\n\t\tfmt.Print(string(data))\r\n\t\tc.Send([]byte(\"pong\\n\"))\r\n\t\twg.Done()\r\n\t})\r\n\r\n\tclient2.OnData(func(c nett.Connection, data []byte) {\r\n\t\tfmt.Print(string(data))\r\n\t\twg.Done()\r\n\t})\r\n\r\n\tclient2.Send([]byte(\"ping\\n\"))\r\n\r\n\twg.Wait() // wait until ping and pong\r\n\tclient2.Close()\r\n\tclient1.Close()\r\n\r\n\t// Output:\r\n\t//ping\r\n\t//pong\r\n}\r\n```\r\n\r\n\r\n### Decode Messages\r\n\r\nYou have to provide a *reader* callback to the *Wrap()* func.  \r\nThis callback will be called in a goroutine and waits until a *[]byte* is returned.  \r\nThe result of this callback will be assumed as a complete message and the *OnData* handler will be called.  \r\nA basic implementation of this handler is the [**ReadLineReader**](https://github.com/v-braun/nett/blob/58d050c19512052eef1daa9020285eb48cdafc1d/nett.go#L40):   \r\n\r\n``` golang\r\n\r\n// ReadLineReader is an reader implementation (see Wrap)\r\n// that reads data line by line from the underlining connection\r\nvar ReadLineReader = func(rawConn net.Conn) ([]byte, error) {\r\n\tnewLine := byte('\\n')\r\n\tbuffer := []byte{}\r\n\treadBuffer := make([]byte, 1)\r\n\tfor {\r\n\t\t_, err := rawConn.Read(readBuffer)\r\n\t\tif err != nil {\r\n\t\t\treturn nil, err\r\n\t\t}\r\n\t\tbuffer = append(buffer, readBuffer[0])\r\n\t\tif readBuffer[0] == newLine {\r\n\t\t\treturn buffer, nil\r\n\t\t}\r\n\t}\r\n}\r\n\r\n```\r\n\r\n\r\n\r\n\r\n## Authors\r\n\r\n![image](https://avatars3.githubusercontent.com/u/4738210?v=3\u0026amp;s=50)  \r\n[v-braun](https://github.com/v-braun/)\r\n\r\n\r\n\r\n## Contributing\r\n\r\nMake sure to read these guides before getting started:\r\n- [Contribution Guidelines](https://github.com/v-braun/nett/blob/master/CONTRIBUTING.md)\r\n- [Code of Conduct](https://github.com/v-braun/nett/blob/master/CODE_OF_CONDUCT.md)\r\n\r\n## License\r\n**nett** is available under the MIT License. See [LICENSE](https://github.com/v-braun/nett/blob/master/LICENSE) for details.\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fv-braun%2Fnett","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fv-braun%2Fnett","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fv-braun%2Fnett/lists"}