mirror of
https://github.com/kataras/iris.git
synced 2025-12-23 12:57:05 +00:00
update websocket examples - keep neffos and use the iris/websocket as helper only
Former-commit-id: c34b916e186286cd0de6d694d8bdb4f9390121a2
This commit is contained in:
@@ -10,6 +10,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/websocket"
|
||||
|
||||
"github.com/kataras/neffos"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -21,17 +23,17 @@ const (
|
||||
// this can be shared with the server.go's.
|
||||
// `NSConn.Conn` has the `IsClient() bool` method which can be used to
|
||||
// check if that's is a client or a server-side callback.
|
||||
var clientEvents = websocket.Namespaces{
|
||||
namespace: websocket.Events{
|
||||
websocket.OnNamespaceConnected: func(c *websocket.NSConn, msg websocket.Message) error {
|
||||
var clientEvents = neffos.Namespaces{
|
||||
namespace: neffos.Events{
|
||||
neffos.OnNamespaceConnected: func(c *neffos.NSConn, msg neffos.Message) error {
|
||||
log.Printf("connected to namespace: %s", msg.Namespace)
|
||||
return nil
|
||||
},
|
||||
websocket.OnNamespaceDisconnect: func(c *websocket.NSConn, msg websocket.Message) error {
|
||||
neffos.OnNamespaceDisconnect: func(c *neffos.NSConn, msg neffos.Message) error {
|
||||
log.Printf("disconnected from namespace: %s", msg.Namespace)
|
||||
return nil
|
||||
},
|
||||
"chat": func(c *websocket.NSConn, msg websocket.Message) error {
|
||||
"chat": func(c *neffos.NSConn, msg neffos.Message) error {
|
||||
log.Printf("%s", string(msg.Body))
|
||||
return nil
|
||||
},
|
||||
@@ -42,7 +44,7 @@ func main() {
|
||||
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(dialAndConnectTimeout))
|
||||
defer cancel()
|
||||
|
||||
client, err := websocket.Dial(ctx, websocket.DefaultGorillaDialer, endpoint, clientEvents)
|
||||
client, err := neffos.Dial(ctx, websocket.DefaultGorillaDialer, endpoint, clientEvents)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -5,26 +5,29 @@ import (
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/websocket"
|
||||
|
||||
"github.com/kataras/neffos"
|
||||
)
|
||||
|
||||
const namespace = "default"
|
||||
|
||||
// if namespace is empty then simply websocket.Events{...} can be used instead.
|
||||
var serverEvents = websocket.Namespaces{
|
||||
namespace: websocket.Events{
|
||||
websocket.OnNamespaceConnected: func(nsConn *websocket.NSConn, msg websocket.Message) error {
|
||||
// if namespace is empty then simply neffos.Events{...} can be used instead.
|
||||
var serverEvents = neffos.Namespaces{
|
||||
namespace: neffos.Events{
|
||||
neffos.OnNamespaceConnected: func(nsConn *neffos.NSConn, msg neffos.Message) error {
|
||||
// with `websocket.GetContext` you can retrieve the Iris' `Context`.
|
||||
ctx := websocket.GetContext(nsConn.Conn)
|
||||
|
||||
log.Printf("[%s] connected to namespace [%s] with IP [%s]",
|
||||
nsConn, msg.Namespace,
|
||||
// with `GetContext` you can retrieve the Iris' `Context`, alternatively
|
||||
// you can use the `nsConn.Conn.Socket().Request()` to get the raw `*http.Request`.
|
||||
websocket.GetContext(nsConn.Conn).RemoteAddr())
|
||||
ctx.RemoteAddr())
|
||||
return nil
|
||||
},
|
||||
websocket.OnNamespaceDisconnect: func(nsConn *websocket.NSConn, msg websocket.Message) error {
|
||||
neffos.OnNamespaceDisconnect: func(nsConn *neffos.NSConn, msg neffos.Message) error {
|
||||
log.Printf("[%s] disconnected from namespace [%s]", nsConn, msg.Namespace)
|
||||
return nil
|
||||
},
|
||||
"chat": func(nsConn *websocket.NSConn, msg websocket.Message) error {
|
||||
"chat": func(nsConn *neffos.NSConn, msg neffos.Message) error {
|
||||
// room.String() returns -> NSConn.String() returns -> Conn.String() returns -> Conn.ID()
|
||||
log.Printf("[%s] sent: %s", nsConn, string(msg.Body))
|
||||
|
||||
@@ -39,7 +42,7 @@ var serverEvents = websocket.Namespaces{
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
websocketServer := websocket.New(
|
||||
websocketServer := neffos.New(
|
||||
websocket.DefaultGorillaUpgrader, /* DefaultGobwasUpgrader can be used too. */
|
||||
serverEvents)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user