Repository metrics
- Stars
- (628 stars)
- PR merge metrics
- (No merged PRs in 30d)
Description
Hi @kataras, I started to use neffos nearly weak ago and i really like it, where i work we decide to move into real time aplication, and i searched for a good socket package, i already used iris and i really like how it works, i was working with websockets of iris and when i updated iris i see some errors (i was in trubles before because updates of other libraries we use), then i read the documentation and i've changed all.
I also integrate the sockets with echo framework and i wold like to leave here how i made it for anyone who need it and also i think it will be good for this greate library if they have examples of implementatios with other to.
Then here is the code:
package main
import (
"github.com/labstack/echo"
"github.com/kataras/neffos"
"github.com/kataras/neffos/gorilla"
"github.com/labstack/echo"
)
type serverConn struct {
*neffos.NSConn
}
func main() {
router := echo.New()
controller := new(serverConn)
events := neffos.NewStruct(controller).
SetNamespace("default").
SetEventMatcher(neffos.EventTrimPrefixMatcher("On"))
websocketServer := neffos.New(
gorilla.DefaultUpgrader, /* DefaultGobwasUpgrader can be used too. */
events)
// Wrap the web socket handler with echo function to be compatible, both use http library to work
router.Any("/echo", echo.WrapHandler(websocketServer), deleteOrigin)
if err := router.Start(":8080"); err != nil {
log.Fatalf("error in ListenAndServe: %s", err)
}
}
// deleteOrigin allows others origins than local access to sockets
// if we avoid this handler then we can't connect from servers in other ports or routes
// NOTE: This isn't the best way to do it, but i can't found another way to do it.
func deleteOrigin(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Request().Header.Del("Origin")
return next(c)
}
}
func (c *serverConn) OnNamespaceConnected(msg neffos.Message) error {
log.Printf("[%s] connected to namespace [%s]",
c.Conn, msg.Namespace)
return nil
}
func (c *serverConn) OnNamespaceDisconnect(msg neffos.Message) error {
log.Printf("[%s] disconnected from namespace [%s]", c.Conn, msg.Namespace)
return nil
}
func (c *serverConn) OnChat(msg neffos.Message) error {
log.Printf("[%s] sent: %s", c.Conn, string(msg.Body))
c.Conn.Server().Broadcast(c.Conn, msg)
return nil
}
This is only for the server file, if anyone want to test it use with the files in the route: _examples/basic/browser
One las thing: I don't know how to acces to the context of the request.