extension-websocket icon indicating copy to clipboard operation
extension-websocket copied to clipboard

Websocket server implementation.

Open Lerg opened this issue 2 years ago • 8 comments

Added non secure websocket server implementation.

Lerg avatar Feb 22 '23 20:02 Lerg

@britzl thank you for testing the code. I will address the issues once I get some free time.

@JCash the http_server.cpp file is not the same from dmSDK, I had to change it heavily to make it work.

Lerg avatar Feb 27 '23 14:02 Lerg

Hi. Anything blocking this from merge?

I am start work on new game and need websocket server for defold.

Also this is not worked for me in windows.

/native/websocket/src/wslay/wslay_frame.c
	Line 79: use of undeclared identifier 'htons'
   79 |       uint16_t len = htons(iocb->payload_length);
      |                      ^

I tried to add "#include <Winsock2.h>" but it not worked( /native/websocket/src/websocket.cpp Line 27: 'Winsock2.h' file not found 27 | #include <Winsock2.h> | ^~~~~~~~~~~~

# C++ symbol in your extension
name: "Websocket"

platforms:
  common:
    context:
        flags:      ["-std=c++11"]
        includes:   ["upload/websocket/include/wslay"]
        defines:    ["HAVE_CONFIG_H"]

  x86_64-win32:
    context:
        libs: ["Ws2_32.lib"]

  x86-win32:
    context:
        libs: ["Ws2_32.lib"]

d954mas avatar Mar 27 '24 15:03 d954mas

Hi. Anything blocking this from merge?

Yes, the issues raised during the review have not been adressed.

britzl avatar Mar 27 '24 15:03 britzl

@d954mas Since we build on Linux, the path casing is extra important. It's not actually called Winsock2.h, it's called WinSock2.h.

JCash avatar Mar 27 '24 16:03 JCash

@d954mas Since we build on Linux, the path casing is extra important. It's not actually called Winsock2.h, it's called WinSock2.h.

Thanks for help. Now it build on windows.

d954mas avatar Mar 27 '24 17:03 d954mas

Also this not build for html5 /native/websocket/src/wslay/wslay_frame.c Line 84: use of undeclared identifier 'htons' uint16_t len = htons(iocb->payload_length); ^

Is server worked in web? If not server should be removed from html?

d954mas avatar Mar 27 '24 17:03 d954mas

I am making prototype for new game with websocket server in defold. I will be write here some problems that i find.

handshake not worked when i try to connect from web build to windows websocket server.

problem with header validation. Server except for "connection_header" "Upgrade" get "keep-alive, Upgrade"


// Helper function to check if a specified token is present in a header value
static bool HasToken(const char* headerValue, const char* tokenToFind) {
    // Create a mutable copy of the headerValue
    char* headerValueCopy = strdup(headerValue);
    if (!headerValueCopy) {
        dmLogError("Can't make str copy for HasToken");
        return false; // Failed to allocate memory for the copy
    }

    char* last; // State pointer for dmStrTok
    dmLogInfo("has token:%s", headerValueCopy);
    char* token = dmStrTok(headerValueCopy, ", ", &last); // Get the first token

    while (token != NULL) {
        dmLogInfo("check token:%s", token);
        if (dmStrCaseCmp(token, tokenToFind) == 0) {
            free(headerValueCopy); // Clean up the allocated memory
            return true; // Found the specified token
        }
        token = dmStrTok(NULL, ", ", &last); // Get the next token
    }

    free(headerValueCopy); // Clean up the allocated memory
    return false; // Specified token not found
}


bool connection = connection_header && dmStrCaseCmp(connection_header->m_Value, "Upgrade") == 0;
bool upgrade  = upgrade_header && dmStrCaseCmp(upgrade_header->m_Value, "websocket") == 0;

bool connection = connection_header && HasToken(connection_header->m_Value, "Upgrade");
bool upgrade = upgrade_header && HasToken(upgrade_header->m_Value, "websocket");
 
 

d954mas avatar Apr 03 '24 16:04 d954mas

Not worked in linux

ERROR: native/websocket/src/wslay/wslay_frame.c:93: 'use of undeclared identifier 'htons'
   93 |       uint16_t len = htons(iocb->payload_length);'

ERROR: native/websocket/src/wslay/wslay_frame.c:93: 'use of undeclared identifier 'htons'
   93 |       uint16_t len = htons(iocb->payload_length);'

ERROR: native/websocket/src/wslay/wslay_frame.c:93: 'use of undeclared identifier 'htons'
   93 |       uint16_t len = htons(iocb->payload_length);'

Need add

#ifdef DM_PLATFORM_LINUX
#include <arpa/inet.h>  // ntohl and htons
#endif

d954mas avatar Apr 18 '24 11:04 d954mas