English | 简体中文
A lightweight TCP Server framework based on Golang.
- Message router
- Route group
- Route middleware
- Message encoding and decoding
- Message processing queue
- Message read-write separation
- Connection heartbeat detection
- Hooks
- Logger
- TLS Support
- WebSocket Support
- Unit test
go get -u github.com/ramzeng/ramix
package main
import (
"github.com/ramzeng/ramix"
"time"
)
func main() {
ramix.SetMode(ramix.DebugMode)
server := ramix.NewServer(
ramix.WithPort(8899),
)
server.UseWorkerPool(ramix.NewRoundRobinWorkerPool(100, 1024))
server.Use(ramix.Recovery(), ramix.Logger())
server.RegisterRoute(0, func(context *ramix.Context) {
_ = context.Connection.SendMessage(context.Request.Message.Event, []byte("pong"))
})
server.Serve()
}
package main
import (
"fmt"
"github.com/ramzeng/ramix"
"net"
"time"
)
func main() {
socket, err := net.Dial("tcp4", "127.0.0.1:8899")
if err != nil {
fmt.Println("Dial error: ", err)
return
}
encoder := ramix.Encoder{}
decoder := ramix.Decoder{}
for {
message := ramix.Message{
Event: 0,
Body: []byte("ping"),
}
message.BodySize = uint32(len(message.Body))
encodedMessage, err := encoder.Encode(message)
if err != nil {
fmt.Println("Encode error: ", err)
return
}
_, err = socket.Write(encodedMessage)
if err != nil {
fmt.Println("Write error: ", err)
return
}
buffer := make([]byte, 1024)
_, err = socket.Read(buffer)
if err != nil {
fmt.Println("Read error: ", err)
return
}
message, err = decoder.Decode(buffer)
if err != nil {
fmt.Println("Decode error: ", err)
return
}
fmt.Printf("Server message: %s\n", message.Body)
time.Sleep(time.Second)
}
}
I am very grateful to JetBrains for providing me with an open-source license for their IDE, which allows me to carry out development work on this project and other open-source projects smoothly.
MIT