2

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 ?

Łukasz Mleczko
  • 173
  • 2
  • 12
  • Found this (https://kripken.github.io/emscripten-site/docs/porting/emscripten-runtime-environment.html) "When all dependencies are met, Emscripten will call run(), which proceeds to call your main() function. The main() function should be used to perform initialization tasks, and will often call emscripten_set_main_loop() (as described above). The main loop function will be then be called at the requested frequency.". It seems you can't have an eternal loop in your client code. – Kjell-Olov Högdahl Jul 14 '16 at 21:52
  • I add while(1) only for debbuging(I search where code shutdown socket immediately, so it is after 'connect' function. Part of code which I added is from SDL2_net lib((file: SDLnet_TCP.c)). Normally while(1) didn't exist here but problem is still here. This is no problem with While – Łukasz Mleczko Jul 15 '16 at 09:02
  • @LukaszML Have you checked your javascript console to make sure there are no errors? The code you posted is probably not the problem. I suspect the main part of your code isn't following the emscripten callback structure or that you are trying to do something with the socket that isn't supported by emscripten (e.g. blocking reads/selects on the socket, etc) – kanaka Jul 15 '16 at 16:04
  • @kanaka I download from https://github.com/emscripten-ports/SDL2_net, next I implement this lib to my project. I create Network.cpp binding to my project: http://pastebin.com/86zzebwA and all working great for dekstop and mobile when next compiled by emscripten doen't work. Connecting but suddenly disconnect, so I try debug, why it doesn't work, and I analizing code and I found this function connect() when program call then connect to server and suddenly disconnect. That is all – Łukasz Mleczko Jul 15 '16 at 16:26
  • @LukaszML please try and get log output. In particular, output from the log::messageln calls will especially help in locating what piece of the code is causing the disconnect. Also, you might try the chat demo in the SDL_net repository and see if you can get that working as a reference: https://github.com/emscripten-ports/SDL2_net – kanaka Jul 15 '16 at 17:21
  • @kanaka I can't locate this code because this is in "connect" function. Connect function is from libc networking precompiled by Emscripten I haven't acces to them. My first server is based on example from this link but didn't work so I try with BeeJ example(above mentioned), not working too :( – Łukasz Mleczko Jul 15 '16 at 17:42
  • @LukaszML please enable SDL_Net logging (https://wiki.libsdl.org/CategoryLog) and show the console output with SDL_Net logging enabled. – kanaka Jul 15 '16 at 19:51
  • Emscripten probably doesn't support SDL Logging. I add for tests((SDL_LogSetAllPriority(SDL_LOG_PRIORITY_WARN); SDL_LogWarn(0, "test log info");) to my code but in emscripten console don't show warn. but Visual studio shown my test warn. – Łukasz Mleczko Jul 15 '16 at 23:26
  • Does this code run in nodejs? I don't think Emscripten will automatically use websockets. You need to write some code to shim websockets onto Emscripten/nodejs socket calls. – 0xcaff Nov 12 '16 at 01:06
  • No, you must create extension in pure javascript next in cpp use EM_ASM_() – Łukasz Mleczko Nov 18 '16 at 21:33

1 Answers1

0

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!

  • I add while(1) only for debbuging(I search where code shutdown socket immediately, so it is after 'connect' function. Part of code which I added is from SDL2_net lib((file: SDLnet_TCP.c)). Normally while(1) didn't exist here but problem is still here. This is no problem with While – Łukasz Mleczko Jul 15 '16 at 09:02
  • I don't have your setup available with me right now. But I still argue that you should return execution to the web browser runtime to ensure you don't starve any runtime process that needs to get execution time to keep the socket connected. What you do is hanging the browser and that is asking for trouble as I see it. If I get round to it I try some socket connect code myself and report back to you if I have any clues to return to you. Good luck! – Kjell-Olov Högdahl Jul 15 '16 at 22:18
  • This is no problem with While. I send you my Network.cpp code: http://pastebin.com/86zzebwA SDLnet_TCP_Open bring me to SDLnet_TCP.c, and I write in my first post bit of code from this file, with while because I just try what gonna happen. Problem still exist when I remove while because this doen't matter in this case. – Łukasz Mleczko Jul 15 '16 at 23:40