Emscripten -> websockify -> raw tcp unexpected hung up

937 Views Asked by At

I have problem with connect to raw TCP server form compiled web-application with emscripten. When I connect from deskopt version of app all works great.

On my VPS I download, compiled and run websockify by:

./websockify 0.0.0.0:1235 127.0.0.1:1234

next I compile and run my server, code: http://pastebin.com/KiehDrvk (from BeeJ networking)

My client code is very simple(only for tests purpose), bit of code:

    TCPsocket sock;
    struct sockaddr_in sock_addr;

    /* Allocate a TCP socket structure */
    sock = (TCPsocket)malloc(sizeof(*sock));
    if ( sock == NULL ) {
        SDLNet_SetError("Out of memory");
        goto error_return;
    }

    /* Open the socket */
    sock->channel = socket(AF_INET, SOCK_STREAM, 0);
    if ( sock->channel == INVALID_SOCKET ) {
        SDLNet_SetError("Couldn't create socket");
        goto error_return;
    }
    /* Connect to remote, or bind locally, as appropriate */ 
    if ( (ip->host != INADDR_NONE) && (ip->host != INADDR_ANY) ) {

    // #########  Connecting to remote

        memset(&sock_addr, 0, sizeof(sock_addr));
        sock_addr.sin_family = AF_INET;
        sock_addr.sin_addr.s_addr = ip->host;
        sock_addr.sin_port = ip->port;

        /* Connect to the remote host */
        if ( connect(sock->channel, (struct sockaddr *)&sock_addr, sizeof(sock_addr)) == SOCKET_ERROR && errno != EINPROGRESS ) {
            SDLNet_SetError("Couldn't connect to remote host");
            goto error_return;
        }
        while (1);
    }

So when I run this on desktop, client connect to server and wait as I expected.

Server Terminal Return:

selectserver: new connection from 91.211.105.49 on socket 5

Websockify Terminal Return:

None because is from desktop

But when I try connect form web version, client connect and suddenly disconnect:

Server Terminal Return:

selectserver: new connection from 127.0.0.1 on socket 6
selectserver: socket 6 hung up
hung up: Success

Websockify Terminal Return:

  1: got client connection from 91.211.105.49
  1: forking handler process
  1: using plain (not SSL) socket
  1: using protocol HyBi/IETF 6455 13
  1: connecting to: 127.0.0.1:1234
  1: client closed connection
  1: handler exit

Someone have any idea ?

1

There are 1 best solutions below

3
On

You must allow your code to return to the web browser runtime. You have a while(1) eternal loop and I suspect your browser kills your app off due to unresponsiveness?

If you are using Emscriptem proposed design for the main loop (https://kripken.github.io/emscripten-site/docs/porting/emscripten-runtime-environment.html#implementing-an-asynchronous-main-loop-in-c-c) you could just move your variables to global scope and remove the while(1) loop from your code.

// (1) put all your variables here (global scope)
int main() {

// (2) put your connect code here (without the while(1) loop)

#ifdef __EMSCRIPTEN__
  // void emscripten_set_main_loop(em_callback_func func, int fps, int simulate_infinite_loop);
  emscripten_set_main_loop(one_iter, 60, 1);
#else
  while (1) {
    one_iter();
    // Delay to keep frame rate constant (using SDL)
    SDL_Delay(time_to_next_frame());
  }
#endif
}

// The "main loop" function.
void one_iter() {
  // process input
  // render to screen
}

This should allow you to at least test your code in the browser. Good luck!