8000 GitHub - WasimAhmad/watsontcp-go: watsontcp-go is a standalone Go implementation of the WatsonTcp C# messaging library. · GitHub
[go: up one dir, main page]

Skip to content

About

watsontcp-go is a standalone Go implementation of the WatsonTcp C# messaging library.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

31 Commits

Folders and files

Repository files navigation

WatsonTcp for Go

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.

Features

  • 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

Installation

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"
)

Basic Usage

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 {}
}

Client

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"))
}

Debug Logging

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)

Examples

The examples directory contains small programs that demonstrate most library features:

  • Client – interactive console client
  • Server – simple console server
  • FileTransfer – streaming data between client and server
  • ClientStream – client sends a stream to the server
  • Metadata – sending messages with metadata maps
  • LargeMessages – validate framing with multi-megabyte payloads
  • Parallel – multiple clients sending concurrently
  • MultiThread – multiple goroutines sending on one client connection
  • Reconnect – reconnect logic for unreliable networks
  • SyncMessages – synchronous request/response messaging
  • Deadlock – demonstrates a send/receive deadlock when both sides wait on each other
  • MaxConnections – demonstrates connection limit enforcement
  • FastDisconnect – connect and immediately disconnect repeatedly

Build an example with:

go build ./examples/<ExampleName>

For the new large message test:

go build ./examples/LargeMessages

Compatibility with C#

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.

About

watsontcp-go is a standalone Go implementation of the WatsonTcp C# messaging library.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages

0