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 · 1 comment
Open

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

illidanstormrange opened this issue Jan 22, 2025 · 1 comment

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

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