WatsonTcp provides a simple TCP messaging framework with message framing, authentication, and connection management. This Go implementation shares the same wire protocol as the C# library and can communicate with it directly.
- Message framing compatible with WatsonTcp for C#
- TLS encryption support
- Optional preshared key authentication
- Idle timeouts and keepalive settings
- Send and receive byte slices or streams
- Synchronous request/response messaging
- Connection filters (allow/deny lists)
- Connection limit enforcement
- Runtime statistics (bytes and messages sent/received)
- Optional debug logging with customizable logger
go get github.com/WasimAhmad/watsontcp-go
Import the packages you need:
import (
"github.com/WasimAhmad/watsontcp-go/client"
"github.com/WasimAhmad/watsontcp-go/server"
)package main
import (
"fmt"
"log"
"github.com/WasimAhmad/watsontcp-go/message"
"github.com/WasimAhmad/watsontcp-go/server"
)
func main() {
cb := server.Callbacks{}
cb.OnMessage = func(id string, msg *message.Message, data []byte) {
fmt.Printf("%s: %s\n", id, string(data))
}
srv := server.New("127.0.0.1:9000", nil, cb, nil)
if err := srv.Start(); err != nil {
log.Fatal(err)
}
defer srv.Stop()
select {}
}package main
import (
"log"
"github.com/WasimAhmad/watsontcp-go/client"
"github.com/WasimAhmad/watsontcp-go/message"
)
func main() {
cb := client.Callbacks{}
cb.OnMessage = func(msg *message.Message, data []byte) {
log.Printf("server: %s", string(data))
}
c := client.New("127.0.0.1:9000", nil, cb, nil)
if err := c.Connect(); err != nil {
log.Fatal(err)
}
defer c.Disconnect()
c.Send(&message.Message{}, []byte("hello"))
}Both the client and server accept Options structures which enable debug
logging. Set DebugMessages to true and provide a Logger function matching
fmt.Printf to receive logs whenever messages are sent or received.
opts := client.DefaultOptions()
opts.Logger = log.Printf
opts.DebugMessages = true
c := client.New("127.0.0.1:9000", nil, cb, &opts)The examples directory contains small programs that demonstrate most
library features:
Client– interactive console clientServer– simple console serverFileTransfer– streaming data between client and serverClientStream– client sends a stream to the serverMetadata– sending messages with metadata mapsLargeMessages– validate framing with multi-megabyte payloadsParallel– multiple clients sending concurrentlyMultiThread– multiple goroutines sending on one client connectionReconnect– reconnect logic for unreliable networksSyncMessages– synchronous request/response messagingDeadlock– demonstrates a send/receive deadlock when both sides wait on each otherMaxConnections– demonstrates connection limit enforcementFastDisconnect– connect and immediately disconnect repeatedly
Build an example with:
go build ./examples/<ExampleName>
For the new large message test:
go build ./examples/LargeMessages
Because the framing and message structure mirror the C# implementation, a Go client can communicate with a C# server and vice versa. Ensure that features such as preshared key authentication are enabled or disabled consistently on both sides.