Skip to content

orkunkaraduman/go-tcpserver

master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

DISCONTINUED

Please visit: github.com/orkunkaraduman/go-accepter

Go TCP Server library

GoDoc

Go TCP Server library provides tcpserver package implements TCP server.

Go TCP Server library has TCPServer struct seems like Server struct of http package to create TCP servers. Also offers TextProtocol struct as a Handler to implement text based protocols.

Examples

For more examples, examples/

examples/go-tcpserver-echoline

package main

import (
	"fmt"
	"log"

	"github.com/orkunkaraduman/go-tcpserver"
)

func main() {
	prt := &tcpserver.TextProtocol{
		OnAccept: func(ctx *tcpserver.TextProtocolContext) {
			ctx.WriteLine("WELCOME")
		},
		OnQuit: func(ctx *tcpserver.TextProtocolContext) {
			ctx.WriteLine("QUIT")
		},
		OnReadLine: func(ctx *tcpserver.TextProtocolContext, line string) int {
			fmt.Println(line)
			if line == "QUIT" {
				ctx.Close()
				return 0
			}
			ctx.WriteLine(line)
			return 0
		},
		OnReadData: func(ctx *tcpserver.TextProtocolContext, data []byte) {

		},
	}
	srv := &tcpserver.TCPServer{
		Addr:    ":1234",
		Handler: prt,
	}
	log.Fatal(srv.ListenAndServe())
}

Releases

No releases published

Packages

No packages published

Languages