2017-12-20 16:56:28 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-07-09 11:16:19 +02:00
|
|
|
"fmt"
|
2017-12-20 16:56:28 +01:00
|
|
|
"sync/atomic"
|
|
|
|
|
|
|
|
"github.com/kataras/iris"
|
|
|
|
"github.com/kataras/iris/mvc"
|
|
|
|
"github.com/kataras/iris/websocket"
|
2019-07-09 11:16:19 +02:00
|
|
|
|
|
|
|
"github.com/kataras/neffos"
|
2017-12-20 16:56:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := iris.New()
|
2019-07-09 11:16:19 +02:00
|
|
|
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())
|
|
|
|
|
2018-01-05 06:57:04 +01:00
|
|
|
// load templates.
|
2017-12-20 16:56:28 +01:00
|
|
|
app.RegisterView(iris.HTML("./views", ".html"))
|
|
|
|
|
2019-07-09 11:16:19 +02:00
|
|
|
// render the ./browser/index.html.
|
|
|
|
app.HandleDir("/", "./browser")
|
2017-12-20 16:56:28 +01:00
|
|
|
|
2019-07-09 11:16:19 +02:00
|
|
|
websocketAPI := app.Party("/websocket")
|
2017-12-20 16:56:28 +01:00
|
|
|
|
2019-07-09 11:16:19 +02:00
|
|
|
m := mvc.New(websocketAPI)
|
|
|
|
m.Register(
|
|
|
|
&prefixedLogger{prefix: "DEV"},
|
|
|
|
)
|
|
|
|
m.HandleWebsocket(&websocketController{Namespace: "default", Age: 42, Otherstring: "other string"})
|
2017-12-20 16:56:28 +01:00
|
|
|
|
2019-07-09 11:16:19 +02:00
|
|
|
websocketServer := neffos.New(websocket.DefaultGorillaUpgrader, m)
|
2017-12-20 16:56:28 +01:00
|
|
|
|
2019-07-09 11:16:19 +02:00
|
|
|
websocketAPI.Get("/", websocket.Handler(websocketServer))
|
|
|
|
// http://localhost:8080
|
|
|
|
app.Run(iris.Addr(":8080"))
|
2017-12-20 16:56:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var visits uint64
|
|
|
|
|
|
|
|
func increment() uint64 {
|
|
|
|
return atomic.AddUint64(&visits, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func decrement() uint64 {
|
|
|
|
return atomic.AddUint64(&visits, ^uint64(0))
|
|
|
|
}
|
|
|
|
|
|
|
|
type websocketController struct {
|
2019-07-09 11:16:19 +02:00
|
|
|
*neffos.NSConn `stateless:"true"`
|
|
|
|
Namespace string
|
|
|
|
Age int
|
|
|
|
Otherstring string
|
|
|
|
|
|
|
|
Logger LoggerService
|
2017-12-20 16:56:28 +01:00
|
|
|
}
|
|
|
|
|
2019-07-09 11:16:19 +02:00
|
|
|
// or
|
|
|
|
// func (c *websocketController) Namespace() string {
|
|
|
|
// return "default"
|
|
|
|
// }
|
|
|
|
|
|
|
|
func (c *websocketController) OnNamespaceDisconnect(msg neffos.Message) error {
|
|
|
|
c.Logger.Log("Disconnected")
|
2017-12-20 16:56:28 +01:00
|
|
|
// visits--
|
|
|
|
newCount := decrement()
|
2019-07-09 11:16:19 +02:00
|
|
|
// This will call the "OnVisit" event on all clients, except the current one,
|
2017-12-20 16:56:28 +01:00
|
|
|
// (it can't because it's left but for any case use this type of design)
|
2019-07-09 11:16:19 +02:00
|
|
|
c.Conn.Server().Broadcast(nil, neffos.Message{
|
|
|
|
Namespace: msg.Namespace,
|
|
|
|
Event: "OnVisit",
|
|
|
|
Body: []byte(fmt.Sprintf("%d", newCount)),
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
2017-12-20 16:56:28 +01:00
|
|
|
}
|
|
|
|
|
2019-07-09 11:16:19 +02:00
|
|
|
func (c *websocketController) OnNamespaceConnected(msg neffos.Message) error {
|
|
|
|
// println("Broadcast prefix is: " + c.BroadcastPrefix)
|
|
|
|
c.Logger.Log("Connected")
|
|
|
|
|
2017-12-20 16:56:28 +01:00
|
|
|
// visits++
|
|
|
|
newCount := increment()
|
|
|
|
|
2019-07-09 11:16:19 +02:00
|
|
|
// This will call the "OnVisit" event on all clients, including the current
|
2017-12-20 16:56:28 +01:00
|
|
|
// with the 'newCount' variable.
|
|
|
|
//
|
|
|
|
// 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.
|
2019-07-09 11:16:19 +02:00
|
|
|
c.Conn.Server().Broadcast(c, neffos.Message{
|
|
|
|
Namespace: msg.Namespace,
|
|
|
|
Event: "OnVisit",
|
|
|
|
Body: []byte(fmt.Sprintf("%d", newCount)),
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *websocketController) OnChat(msg neffos.Message) error {
|
|
|
|
ctx := websocket.GetContext(c.Conn)
|
|
|
|
|
|
|
|
ctx.Application().Logger().Infof("[IP: %s] [ID: %s] broadcast to other clients the message [%s]",
|
|
|
|
ctx.RemoteAddr(), c, string(msg.Body))
|
|
|
|
|
|
|
|
c.Conn.Server().Broadcast(c, msg)
|
|
|
|
|
|
|
|
return nil
|
2017-12-20 16:56:28 +01:00
|
|
|
}
|
|
|
|
|
2019-07-09 11:16:19 +02:00
|
|
|
type LoggerService interface {
|
|
|
|
Log(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
type prefixedLogger struct {
|
|
|
|
prefix string
|
|
|
|
}
|
2017-12-20 16:56:28 +01:00
|
|
|
|
2019-07-09 11:16:19 +02:00
|
|
|
func (s *prefixedLogger) Log(msg string) {
|
|
|
|
fmt.Printf("%s: %s\n", s.prefix, msg)
|
2017-12-20 16:56:28 +01:00
|
|
|
}
|