A simple lightweight network framework for Go. It is useful for long connection applications such as IOT
, Chatroom
, Online Game
, Instant Messaging
Websocket
is supported (TCP
,MQTT
maybe later)- distributed architecture and can be scale out
- handle 1 million connections
- use the
CloudEvents 1.0 specification
as event format - support
JSON
,XML
,ProtoBuf
as content type - Golang style
- support middleware chain
go get -u github.com/nite-coder/prelude
func main() {
opts := hubNATS.HubOptions{
URL: "nats://nats:4222",
Group: "gateway",
}
// we use nats as mq
hub, err := hubNATS.NewNatsHub(opts)
if err != nil {
panic(err)
}
router := prelude.NewRouter(hub)
router.AddRoute("ping", func(c *prelude.Context) error {
// handle ping command here
return c.JSON("pong", "hello world") // the event will send back to client
})
websocketGateway := websocket.NewGateway()
err = websocketGateway.ListenAndServe(":10080", hub)
if err != nil {
log.Err(err).Error("main: websocket gateway start failed")
}
}
func main() {
ws, _, err := websocket.DefaultDialer.Dial("ws://127.0.0.1:10080", nil)
if err != nil {
t.Fatalf("%v", err)
}
defer ws.Close()
pingEvent := cloudevents.NewEvent()
pingEvent.SetID(uuid.NewString())
pingEvent.SetSource("client")
pingEvent.SetType("ping")
ws.WriteJSON(pingEvent)
}