Skip to content

Commit c069c07

Browse files
committed
fix: don't send to caller
1 parent 7abf7a7 commit c069c07

2 files changed

Lines changed: 58 additions & 4 deletions

File tree

nsocket.go

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func New(config Config) *NSocket {
197197
})
198198

199199
nsoc.melody.HandleDisconnect(func(s *melody.Session) {
200-
fmt.Printf("Client :%v Disconnected\n", s.RemoteAddr())
200+
fmt.Printf("Client :%v Disconnected -- %s \n", s.RemoteAddr(), s.Request.Context().Err())
201201
nsoc.unsubscribeFromAll(s)
202202
events := nsoc.config.Namespace[Default]
203203
f, ok := events[resolveNamespace(OnNamespaceDisconnect)]
@@ -234,14 +234,34 @@ func New(config Config) *NSocket {
234234
return nsoc
235235
}
236236

237-
func (nsoc *NSocket) Emit(v interface{}, namespace string) (err error) {
237+
func (nsoc *NSocket) Emit(v interface{}, i ...interface{}) (err error) {
238+
var namespace string
239+
var s *melody.Session
240+
241+
for k := 0; k < 2; k++ {
242+
switch i[k].(type) {
243+
case *melody.Session:
244+
s = i[k].(*melody.Session)
245+
case string:
246+
namespace = i[k].(string)
247+
}
248+
}
238249
nsoc.rwMutex.Lock()
239250
ns := resolveNamespace(namespace)
240251
sess, ok := nsoc.namespaces[ns]
241252
if !ok {
242253
return fmt.Errorf("namespace not found")
243254
}
244255
nsoc.rwMutex.Unlock()
256+
if s != nil {
257+
tmp := sess
258+
sess = []*melody.Session{}
259+
for _, ss := range tmp {
260+
if ss != s {
261+
sess = append(sess, s)
262+
}
263+
}
264+
}
245265
var b []byte
246266
if v != nil {
247267
b, err = json.Marshal(Message{
@@ -257,7 +277,41 @@ func (nsoc *NSocket) Emit(v interface{}, namespace string) (err error) {
257277
return nsoc.melody.BroadcastMultiple(b, sess)
258278
}
259279

260-
func (nsoc *NSocket) Broadcast(v interface{}) (err error) {
280+
func (nsoc *NSocket) EmitAll(v interface{}, namespace string) (err error) {
281+
nsoc.rwMutex.Lock()
282+
ns := resolveNamespace(namespace)
283+
sess, ok := nsoc.namespaces[ns]
284+
if !ok {
285+
return fmt.Errorf("namespace not found")
286+
}
287+
nsoc.rwMutex.Unlock()
288+
var b []byte
289+
if v != nil {
290+
b, err = json.Marshal(Message{
291+
Id: uuid.New(),
292+
Type: _EMIT_,
293+
Body: v,
294+
Namespace: namespace,
295+
})
296+
if err != nil {
297+
return
298+
}
299+
}
300+
return nsoc.melody.BroadcastMultiple(b, sess)
301+
}
302+
303+
func (nsoc *NSocket) Broadcast(v interface{}, s *melody.Session) (err error) {
304+
var b []byte
305+
if v != nil {
306+
b, err = json.Marshal(v)
307+
if err != nil {
308+
return
309+
}
310+
}
311+
return nsoc.melody.BroadcastOthers(b, s)
312+
}
313+
314+
func (nsoc *NSocket) BroadcastAll(v interface{}) (err error) {
261315
var b []byte
262316
if v != nil {
263317
b, err = json.Marshal(v)

nsocket_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestNSocket(t *testing.T) {
4242
nsocket.Default: nsocket.Event{
4343
"/": func(s *melody.Session, i interface{}, soc *nsocket.NSocket) {
4444
fmt.Printf("Namespace: [Default] -- GOT: %v ---- from ---- %v\n", i, s.RemoteAddr())
45-
if err := soc.Broadcast("namespace:default -- " + fmt.Sprintf("%v ------> %v", i, s.RemoteAddr())); err != nil {
45+
if err := soc.Broadcast("namespace:default -- "+fmt.Sprintf("%v ------> %v", i, s.RemoteAddr()), s); err != nil {
4646
t.Error(err)
4747
}
4848
},

0 commit comments

Comments
 (0)