kataras / neffos

A modern, fast and scalable websocket framework with elegant API written in Go
http://bit.ly/neffos-wiki
MIT License
572 stars 47 forks source link

How incoming connections to join room `chat` ? #60

Closed vnaki closed 2 years ago

vnaki commented 2 years ago

1. I expect incoming connections to join room chat, but it doesn't seem so:

package ws

import (
    "context"
    "fmt"
    "github.com/kataras/iris/v12/websocket"
    "time"
)

type BuildService struct {
    *websocket.NSConn `stateless:"true"` 
        *room websocket.Room
}

func NewBuildService() *BuildService {
    return new(BuildService)
}

func (w *BuildService) Namespace() string {
    return "ws.build"
}

func (w *BuildService) OnNamespaceConnected(_ websocket.Message) error {
        //  join room 'chat'
    room, err := w.JoinRoom(context.Background(), "chat")
    if err != nil {
        return err
    }

        w.room = room
    return nil
}

func (w *BuildService) OnNamespaceDisconnect(_ websocket.Message) error {
    if w.room != nil {
        if err := w.room.Leave(context.Background()); err != nil {
            return err
        }
     }
    return nil
}

//   receive  client message 
func (w *BuildService) OnMessage(m websocket.Message) error {
        // reply client message 
    w.Room("chat").Emit("OnMessage", m.Body)
    return nil
}

2. Why rooms is map[string]*Room , not map[string][] *Room ?

type NSConn struct {
    ...

    rooms      map[string]*Room 

    ...
}

each room should have many connections! am I right ?

Can you provide some room detail usage