Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HELP] Multiple event when using native message #2285

Closed
heliosgo opened this issue Nov 16, 2023 · 0 comments
Closed

[HELP] Multiple event when using native message #2285

heliosgo opened this issue Nov 16, 2023 · 0 comments

Comments

@heliosgo
Copy link

Hi, when using websocket, multiple events are set in empty namespace. The server received a namespace connection request, and send a message in its OnNamespaceConnected, the client may panic because ns, ok = c.connectedNamespaces[namespace] is not executed. I'd like to know if this is a bug or if there's something wrong with my usage. Sorry for my bad English

Here is the sample code.

var events = neffos.Events{
	neffos.OnNativeMessage: func(c *neffos.NSConn, msg neffos.Message) error {
		log.Printf("Got: %s", string(msg.Body))

		if !c.Conn.IsClient() {
			return c.Conn.Socket().WriteText(pongMessage, 0)
		}

		return nil
	},
	neffos.OnNamespaceConnected: func(c *neffos.NSConn, msg neffos.Message) error {
		return nil
	},

}

func runClient() {
	ctx := context.Background()

	client, err := neffos.Dial(ctx, gorilla.DefaultDialer, "ws://localhost:8080/endpoint", events)
	if err != nil {
		panic(err)
	}

	c, err := client.Connect(ctx, "")
	if err != nil {
		panic(err)
	}

	c.Conn.Socket().WriteText([]byte("ping"), 0)

	<-client.NotifyClose
}

Here is some source code I found.

func (c *Conn) askConnect(ctx context.Context, namespace string) (*NSConn, error) {

	......

	// println("ask connect")
	_, err = c.Ask(ctx, connectMessage) // waits for answer no matter if already connected on the other side.
	if err != nil {
		return nil, err
	}
	// println("got connect")
	// re-check, maybe connected so far (can happen by a simultaneously `Connect` calls on both server and client, which is not the standard way)
	// c.connectedNamespacesMutex.RLock()
	// ns, ok = c.connectedNamespaces[namespace]
	// c.connectedNamespacesMutex.RUnlock()
	// if ok {
	// 	return ns, nil
	// }

        // ******************************************************************************************
	//  The message was received before executing the following code
        // ******************************************************************************************
	c.connectedNamespacesMutex.Lock()
	c.connectedNamespaces[namespace] = ns
	c.connectedNamespacesMutex.Unlock()

	// println("we're connected")

	// c.writeEmptyReply(genWaitConfirmation(reply.wait))
	// println("wrote: " + genWaitConfirmation(reply.wait))

	// c.sendConfirmation(reply.wait)

	c.notifyNamespaceConnected(ns, connectMessage)
	return ns, nil
}

return ns.events.fireEvent(ns, msg) ns.events is nil

func (c *Conn) handleMessage(msg Message) error {
	if msg.isInvalid {
		return ErrInvalidPayload
	}

	if msg.IsNative && c.allowNativeMessages {
		ns := c.Namespace("")
		return ns.events.fireEvent(ns, msg)   // panic
	}

        ......

	return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant