Skip to content

EmscriptenWebSocketMessageEvent->socket is same for two different socket #22376

@dpoizl-atomontage

Description

@dpoizl-atomontage

Please include the following in your bug report:

Version of emscripten/emsdk:
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.64 (a1fe390)
clang version 19.0.0git (https:/github.com/llvm/llvm-project 4d8e42ea6a89c73f90941fd1b6e899912e31dd34)
Target: wasm32-unknown-emscripten
Thread model: posix

Failing command line in full:
em++ main.cpp -o test.html -sALLOW_MEMORY_GROWTH=1 -lwebsocket

#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#include <cmath>
#include <iostream>
#include <emscripten.h>
#include <emscripten/html5.h>
#include <emscripten/websocket.h>

class TcpSocket
{
	EMSCRIPTEN_WEBSOCKET_T socket;
public:
TcpSocket(int port)
{
	EmscriptenWebSocketCreateAttributes attributes;
	emscripten_websocket_init_create_attributes(&attributes);
	char url[200];
	sprintf(url, "ws://localhost:%d", port);
	attributes.url = url;
	attributes.protocols = "binary";
	attributes.createOnMainThread = EM_TRUE;
	socket = emscripten_websocket_new(&attributes);
	bool registrationSuccessful = true;
	registrationSuccessful &= (emscripten_websocket_set_onopen_callback(socket, this, OnOpen) == 0);
	registrationSuccessful &= (emscripten_websocket_set_onclose_callback(socket, this, OnClose) == 0);
	registrationSuccessful &= (emscripten_websocket_set_onerror_callback(socket, this, OnError) == 0);
	registrationSuccessful &= (emscripten_websocket_set_onmessage_callback(socket, this, OnMessage) == 0);
	printf("registration %s\n", registrationSuccessful ? "successfull" : "failed");
	
	printf("socket open %d %s\n", socket, url);
}
static EM_BOOL OnMessage(int eventType, const EmscriptenWebSocketMessageEvent *messageEvent, void *network)
{
	printf("OnMessage %d socket %d numbytes %d %p\n", eventType, messageEvent->socket, messageEvent->numBytes, network);
	return 0;
}
static EM_BOOL OnClose(int eventType, const EmscriptenWebSocketCloseEvent *closeEvent, void *network)
{
	printf("OnClose %d socket %d %p\n", eventType, closeEvent->socket, network);
	return 0;
}
static EM_BOOL OnError(int eventType, const EmscriptenWebSocketErrorEvent *errorEvent, void *network)
{
	printf("OnClose %d %p\n", eventType, network);
	return 0;
}
static EM_BOOL OnOpen(int eventType, const EmscriptenWebSocketOpenEvent *openEvent, void *network)
{
	printf("OnOpen %d socket %d %p\n", eventType, openEvent->socket, network);
	return 0;
}
};

EM_BOOL one_iter(double time, void* userData)
{
	return EM_TRUE;
}


int main()
{	
	TcpSocket *sock1 = new TcpSocket(4444);
	TcpSocket *sock2 = new TcpSocket(4445);
	emscripten_request_animation_frame_loop(one_iter, 0);
	
	return 0;
}

example of output. The socket is always 2 while in older emscripten 3.1.39 it would be 1 and 2 as you would expect.

registration successfull
socket open 1 ws://localhost:4444
registration successfull
socket open 2 ws://localhost:4445
OnOpen 0 socket 2 0x11398
OnOpen 0 socket 2 0x115b8
OnMessage 0 socket 2 numbytes 49 0x11398
OnMessage 0 socket 2 numbytes 27 0x115b8
OnMessage 0 socket 2 numbytes 15 0x11398
OnMessage 0 socket 2 numbytes 19 0x115b8
OnClose 0 socket 2 0x11398
OnClose 0 socket 2 0x115b8

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions