@@ -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 )
0 commit comments