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

About the use of tcp_echo_client.c and tcp_echo_server.p #23468

Open
illidanstormrange opened this issue Jan 22, 2025 · 3 comments
Open

About the use of tcp_echo_client.c and tcp_echo_server.p #23468

illidanstormrange opened this issue Jan 22, 2025 · 3 comments

Comments

@illidanstormrange
Copy link

illidanstormrange commented Jan 22, 2025

Hello!

When I run tcp_echo_client.c, I cannot connect to the server run by tcp_echo_server.py. The server can print the connection request, but emscripten_websocket_get_ready_state always returns 0, and the connection cannot be established correctly.

Does it mean that as a bridge, it can only connect to the websocket server, but not to TCP? Then why does your sample code have both websocket connection and connect(socket)? If you see this, please help me solve my doubts. Thank you.

I used cmake to compile, here is my CMake

cmake_minimum_required(VERSION 3.6)
project(test_main)
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
set(CMAKE_CXX_STANDARD 11)

if (DEFINED EMSCRIPTEN)
    list(APPEND LINK_OPTIONS -sWASM=1 -sPROXY_POSIX_SOCKETS=1 -lwebsocket.js -sUSE_PTHREADS=1)
    # list(APPEND LINK_OPTIONS -sWASM=1)
endif ()

set(CMAKE_EXECUTABLE_SUFFIX ".html") # 编译生成.html

add_executable(test_main test_emscripten.cpp)
target_link_options(test_main PUBLIC ${LINK_OPTIONS})
@sbc100
Copy link
Collaborator

sbc100 commented Jan 22, 2025

Does it mean that as a bridge, it can only connect to the websocket server, but not to TCP? Then why does your sample code have both websocket connection and connect(socket)? If you see this, please help me solve my doubts. Thank you.

I'll try to answer this question specifically: When you use PROXY_POSIX_SOCKETS all socket communication (i.e. all TCP/UDP connections from the C/C++ code) are proxied (i.e. tunneled) through a single websocket connection that you need to run on your server.

On one of the websocket is you web application, on the other end (i.e. on your web server) you need run a proxying server. The source code for this is here: https://github.com/emscripten-core/emscripten/tree/main/tools/websocket_to_posix_proxy

See https://emscripten.org/docs/porting/networking.html#full-posix-sockets-over-websocket-proxy-server

@illidanstormrange
Copy link
Author

illidanstormrange commented Jan 23, 2025

Thank you for your reply. I found the previous problem after reading this code.
But i ran the proxy server and tried to connect using tcp_echo_client,unfortunately, I got the same result. emscripten_websocket_get_ready_state always returned 0. When I checked ws through the browser, it did not receive the handshake successfully.

Image

Image

@illidanstormrange
Copy link
Author

When I create a websocket client with the following code, it can receive the handshake, but there will be a connection problem

#include <emscripten.h>
#include <emscripten/websocket.h>
#include <stdio.h>
 
EM_BOOL onopen(int eventType, const EmscriptenWebSocketOpenEvent *websocketEvent, void *userData) {
    puts("onopen");
    EMSCRIPTEN_RESULT result = emscripten_websocket_send_utf8_text(websocketEvent->socket, "hello world");
    if (result) {
        printf("Failed to emscripten_websocket_send_utf8_text(): %d\n", result);
    }
    return EM_TRUE;
}
 
EM_BOOL onerror(int eventType, const EmscriptenWebSocketErrorEvent *websocketEvent, void *userData) {
    puts("onerror");
    return EM_TRUE;
}
 
EM_BOOL onclose(int eventType, const EmscriptenWebSocketCloseEvent *websocketEvent, void *userData) {
    puts("onclose");
    return EM_TRUE;
}
 
EM_BOOL onmessage(int eventType, const EmscriptenWebSocketMessageEvent *websocketEvent, void *userData) {
    puts("onmessage");
    if (websocketEvent->isText) {
        printf("message: %s\n", websocketEvent->data);
    }
    EMSCRIPTEN_RESULT result = emscripten_websocket_close(websocketEvent->socket, 1000, "no reason");
    if (result) {
        printf("Failed to emscripten_websocket_close(): %d\n", result);
    }
    return EM_TRUE;
}
 
int main() {
    if (!emscripten_websocket_is_supported()) {
        return 0;
    }
 
    EmscriptenWebSocketCreateAttributes ws_attrs = {"ws://localhost:8080", NULL, EM_TRUE};
    EMSCRIPTEN_WEBSOCKET_T ws = emscripten_websocket_new(&ws_attrs);
 
    emscripten_websocket_set_onopen_callback(ws, NULL, onopen);
    emscripten_websocket_set_onerror_callback(ws, NULL, onerror);
    emscripten_websocket_set_onclose_callback(ws, NULL, onclose);
    emscripten_websocket_set_onmessage_callback(ws, NULL, onmessage);
 
    return 0;
}

I can see in the browser that the ws connection has been established
Image

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

2 participants