https://github.com/antosmichael07/go-tcp-connection
A library meant to simplify the process of making TCP request and responses.
https://github.com/antosmichael07/go-tcp-connection
client connection go golang server tcp tcp-client tcp-server tcp-socket
Last synced: 11 months ago
JSON representation
A library meant to simplify the process of making TCP request and responses.
- Host: GitHub
- URL: https://github.com/antosmichael07/go-tcp-connection
- Owner: antosmichael07
- Created: 2024-06-04T17:48:25.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-12-22T08:36:30.000Z (over 1 year ago)
- Last Synced: 2025-02-24T01:51:28.508Z (over 1 year ago)
- Topics: client, connection, go, golang, server, tcp, tcp-client, tcp-server, tcp-socket
- Language: Go
- Homepage:
- Size: 72.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Go-TCP-Connection
- A library meant to simplify the process of making TCP/UDP request and responses
- Install with `go get github.com/antosmichael07/Go-TCP-Connection`
## Rules
- Your events need to start from 4, events from 0 to 3 are reserved for client to server communication
## Example
### Server
```go
package main
import (
"fmt"
lgr "github.com/antosmichael07/Go-Logger"
tcp "github.com/antosmichael07/Go-TCP-Connection"
)
const (
event_test uint16 = iota + 4
)
func main() {
server := tcp.NewServer("localhost:8080", "tcp")
server.Logger.Level = lgr.Warning
server.OnConnect(func(conn *tcp.Connection) {
msg := []byte("Hello from the server")
server.SendData(conn, event_test, &msg)
})
server.On(event_test, func(data *[]byte, conn *tcp.Connection) {
fmt.Println("Received data from client:", string(*data))
})
server.Start()
}
```
### Client
```go
package main
import (
"fmt"
lgr "github.com/antosmichael07/Go-Logger"
tcp "github.com/antosmichael07/Go-TCP-Connection"
)
const (
event_test uint16 = iota + 4
)
func main() {
client := tcp.NewClient("localhost:8080", "tcp")
client.Logger.Level = lgr.Warning
client.Connect()
client.On(event_test, func(data *[]byte) {
fmt.Println("Received data from the server: ", string(*data))
msg := []byte("Hello from the client")
client.SendData(event_test, &msg)
})
client.Listen()
}
```