mirror of
https://github.com/defold/extension-websocket.git
synced 2025-06-27 01:47:42 +02:00
Issue 16: Added protocol as a connection parameter
This commit is contained in:
parent
94bcc82f25
commit
1cc1d802b3
@ -66,6 +66,12 @@ static Result SendClientHandshakeHeaders(WebsocketConnection* conn)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (conn->m_Protocol) {
|
||||||
|
WS_SENDALL("Sec-WebSocket-Protocol: ");
|
||||||
|
WS_SENDALL(conn->m_Protocol);
|
||||||
|
WS_SENDALL("\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
WS_SENDALL("\r\n");
|
WS_SENDALL("\r\n");
|
||||||
|
|
||||||
bail:
|
bail:
|
||||||
@ -114,7 +120,7 @@ Result ReceiveHeaders(WebsocketConnection* conn)
|
|||||||
{
|
{
|
||||||
if (dmSocket::RESULT_WOULDBLOCK)
|
if (dmSocket::RESULT_WOULDBLOCK)
|
||||||
{
|
{
|
||||||
DebugLog(1, "Waiting for socket to be available for reading");
|
DebugLog(2, "Waiting for socket to be available for reading");
|
||||||
return RESULT_WOULDBLOCK;
|
return RESULT_WOULDBLOCK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,9 +203,6 @@ Result VerifyHeaders(WebsocketConnection* conn)
|
|||||||
bool connection = false;
|
bool connection = false;
|
||||||
bool upgrade = false;
|
bool upgrade = false;
|
||||||
bool valid_key = false;
|
bool valid_key = false;
|
||||||
const char* protocol = "";
|
|
||||||
|
|
||||||
// TODO: Perhaps also support the Sec-WebSocket-Protocol
|
|
||||||
|
|
||||||
// parse the headers in place
|
// parse the headers in place
|
||||||
while (r < endtag)
|
while (r < endtag)
|
||||||
|
@ -212,6 +212,7 @@ static void DestroyConnection(WebsocketConnection* conn)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
free((void*)conn->m_CustomHeaders);
|
free((void*)conn->m_CustomHeaders);
|
||||||
|
free((void*)conn->m_Protocol);
|
||||||
|
|
||||||
if (conn->m_Callback)
|
if (conn->m_Callback)
|
||||||
dmScript::DestroyCallback(conn->m_Callback);
|
dmScript::DestroyCallback(conn->m_Callback);
|
||||||
@ -271,6 +272,7 @@ static int LuaConnect(lua_State* L)
|
|||||||
const char* url = luaL_checkstring(L, 1);
|
const char* url = luaL_checkstring(L, 1);
|
||||||
lua_Number timeout = dmScript::CheckTableNumber(L, 2, "timeout", 3000);
|
lua_Number timeout = dmScript::CheckTableNumber(L, 2, "timeout", 3000);
|
||||||
const char* custom_headers = dmScript::CheckTableString(L, 2, "headers", 0);
|
const char* custom_headers = dmScript::CheckTableString(L, 2, "headers", 0);
|
||||||
|
const char* protocol = dmScript::CheckTableString(L, 2, "protocol", 0);
|
||||||
|
|
||||||
if (custom_headers != 0)
|
if (custom_headers != 0)
|
||||||
{
|
{
|
||||||
@ -283,6 +285,7 @@ static int LuaConnect(lua_State* L)
|
|||||||
WebsocketConnection* conn = CreateConnection(url);
|
WebsocketConnection* conn = CreateConnection(url);
|
||||||
conn->m_ConnectTimeout = dmTime::GetTime() + timeout * 1000;
|
conn->m_ConnectTimeout = dmTime::GetTime() + timeout * 1000;
|
||||||
conn->m_CustomHeaders = custom_headers ? strdup(custom_headers) : 0;
|
conn->m_CustomHeaders = custom_headers ? strdup(custom_headers) : 0;
|
||||||
|
conn->m_Protocol = protocol ? strdup(protocol) : 0;
|
||||||
|
|
||||||
conn->m_Callback = dmScript::CreateCallback(L, 3);
|
conn->m_Callback = dmScript::CreateCallback(L, 3);
|
||||||
|
|
||||||
@ -709,6 +712,13 @@ static dmExtension::Result OnUpdate(dmExtension::Params* params)
|
|||||||
#if defined(__EMSCRIPTEN__)
|
#if defined(__EMSCRIPTEN__)
|
||||||
conn->m_SSLSocket = dmSSLSocket::INVALID_SOCKET_HANDLE;
|
conn->m_SSLSocket = dmSSLSocket::INVALID_SOCKET_HANDLE;
|
||||||
|
|
||||||
|
if (conn->m_Protocol) {
|
||||||
|
EM_ASM({
|
||||||
|
// https://emscripten.org/docs/porting/networking.html#emulated-posix-tcp-sockets-over-websockets
|
||||||
|
Module["websocket"]["subprotocol"] = UTF8ToString($0);
|
||||||
|
}, conn->m_Protocol);
|
||||||
|
}
|
||||||
|
|
||||||
char uri_buffer[dmURI::MAX_URI_LEN];
|
char uri_buffer[dmURI::MAX_URI_LEN];
|
||||||
const char* uri;
|
const char* uri;
|
||||||
if (conn->m_Url.m_Path[0] != '\0') {
|
if (conn->m_Url.m_Path[0] != '\0') {
|
||||||
|
@ -91,6 +91,7 @@ namespace dmWebsocket
|
|||||||
dmArray<Message> m_Messages; // lengths of the messages in the data buffer
|
dmArray<Message> m_Messages; // lengths of the messages in the data buffer
|
||||||
uint64_t m_ConnectTimeout;
|
uint64_t m_ConnectTimeout;
|
||||||
uint8_t m_Key[16];
|
uint8_t m_Key[16];
|
||||||
|
const char* m_Protocol;
|
||||||
const char* m_CustomHeaders;
|
const char* m_CustomHeaders;
|
||||||
State m_State;
|
State m_State;
|
||||||
char* m_Buffer;
|
char* m_Buffer;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user