1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 10:27:06 +00:00

add the new neffos StackExchange feature to the type aliases and shortcuts of the websocket subpackage and auto-enable debug mode on websocket MVC application when iris logger's level is set to debug

Former-commit-id: 4d8cb79d01a4172fc1ed7a9b626da0228d902b3c
This commit is contained in:
Gerasimos (Makis) Maropoulos
2019-07-11 12:59:11 +03:00
parent 82d645e3cb
commit 33dfb42d73
10 changed files with 83 additions and 57 deletions

View File

@@ -7,18 +7,12 @@ import (
"github.com/kataras/iris"
"github.com/kataras/iris/mvc"
"github.com/kataras/iris/websocket"
"github.com/kataras/neffos"
)
func main() {
app := iris.New()
app.Logger().SetLevel("debug")
// optionally enable debug messages to the neffos real-time framework
// and print them through the iris' logger.
neffos.EnableDebug(app.Logger())
// load templates.
app.RegisterView(iris.HTML("./views", ".html"))
@@ -33,7 +27,7 @@ func main() {
)
m.HandleWebsocket(&websocketController{Namespace: "default", Age: 42, Otherstring: "other string"})
websocketServer := neffos.New(websocket.DefaultGorillaUpgrader, m)
websocketServer := websocket.New(websocket.DefaultGorillaUpgrader, m)
websocketAPI.Get("/", websocket.Handler(websocketServer))
// http://localhost:8080
@@ -51,10 +45,10 @@ func decrement() uint64 {
}
type websocketController struct {
*neffos.NSConn `stateless:"true"`
Namespace string
Age int
Otherstring string
*websocket.NSConn `stateless:"true"`
Namespace string
Age int
Otherstring string
Logger LoggerService
}
@@ -64,13 +58,13 @@ type websocketController struct {
// return "default"
// }
func (c *websocketController) OnNamespaceDisconnect(msg neffos.Message) error {
func (c *websocketController) OnNamespaceDisconnect(msg websocket.Message) error {
c.Logger.Log("Disconnected")
// visits--
newCount := decrement()
// This will call the "OnVisit" event on all clients, except the current one,
// (it can't because it's left but for any case use this type of design)
c.Conn.Server().Broadcast(nil, neffos.Message{
c.Conn.Server().Broadcast(nil, websocket.Message{
Namespace: msg.Namespace,
Event: "OnVisit",
Body: []byte(fmt.Sprintf("%d", newCount)),
@@ -79,7 +73,7 @@ func (c *websocketController) OnNamespaceDisconnect(msg neffos.Message) error {
return nil
}
func (c *websocketController) OnNamespaceConnected(msg neffos.Message) error {
func (c *websocketController) OnNamespaceConnected(msg websocket.Message) error {
// println("Broadcast prefix is: " + c.BroadcastPrefix)
c.Logger.Log("Connected")
@@ -91,7 +85,7 @@ func (c *websocketController) OnNamespaceConnected(msg neffos.Message) error {
//
// There are many ways that u can do it and faster, for example u can just send a new visitor
// and client can increment itself, but here we are just "showcasing" the websocket controller.
c.Conn.Server().Broadcast(nil, neffos.Message{
c.Conn.Server().Broadcast(nil, websocket.Message{
Namespace: msg.Namespace,
Event: "OnVisit",
Body: []byte(fmt.Sprintf("%d", newCount)),
@@ -100,7 +94,7 @@ func (c *websocketController) OnNamespaceConnected(msg neffos.Message) error {
return nil
}
func (c *websocketController) OnChat(msg neffos.Message) error {
func (c *websocketController) OnChat(msg websocket.Message) error {
ctx := websocket.GetContext(c.Conn)
ctx.Application().Logger().Infof("[IP: %s] [ID: %s] broadcast to other clients the message [%s]",