14 Commits
1.4.0 ... 1.5.2

Author SHA1 Message Date
Björn Ritzl
f5aa57452f Merge pull request #26 from defold/Issue-25-crash-in-callback
Destroy connections when finalizing the extension
2020-12-22 12:21:20 +01:00
Björn Ritzl
7e89b8a685 Destroy connections when finalizing the extension 2020-12-22 12:19:12 +01:00
Mathias Westerdahl
ba7454a431 Merge pull request #24 from defold/issue-22-skip-protocol-by-default
Issue 22: Don't send Sec-WebSocket-Protocol unless specified
2020-11-10 08:42:30 +01:00
JCash
b93a91c9b8 Issue 22: Don't send Sec-WebSocket-Protocol unless specified 2020-11-09 19:03:27 +01:00
Mathias Westerdahl
0bf63cbdf8 Added credit and link to wslay 2020-11-01 09:28:27 +01:00
Mathias Westerdahl
8a9dc759a4 Merge pull request #21 from defold/issue-18-error-message-fix
Issue 18: Documentation fix
2020-10-31 15:48:47 +01:00
Mathias Westerdahl
70afde4cfa Merge pull request #20 from defold/issue-17-path-fix
Issue 17: Fixed path argument in http handshake
2020-10-31 15:48:21 +01:00
Mathias Westerdahl
ed6d131470 Merge pull request #19 from defold/issue-16-conneciton-protocol
Issue 16: Added protocol as a connection parameter
2020-10-31 15:47:58 +01:00
JCash
2c9c0b74f1 Issue 18: Documentation fix 2020-10-31 10:55:36 +01:00
JCash
8450a88640 Issue 17: Fixed path argument in http handshake 2020-10-31 10:40:21 +01:00
JCash
3636ea73db added documentation 2020-10-31 10:18:50 +01:00
JCash
1cc1d802b3 Issue 16: Added protocol as a connection parameter 2020-10-31 10:14:44 +01:00
JCash
94bcc82f25 cleanup 2020-10-18 09:42:53 +02:00
JCash
d62a53cbcc Doc fix 2020-10-18 09:42:40 +02:00
5 changed files with 42 additions and 19 deletions

View File

@@ -40,3 +40,9 @@ For command line debugging, there's
* tcpdump: `sudo tcpdump -X -s0 -ilo0 port 8080 ` (example for local ws:// connection)
* tcpdump: `sudo tcpdump -X -s0 host echo.websocket.org` (Monitors packets to/from echo.websocket.org)
## Credits
This extension makes use of the C library WSlay by @tatsuhiro-t:
* https://github.com/tatsuhiro-t/wslay

View File

@@ -19,7 +19,11 @@
members:
- name: timeout
type: number
desc: Timeout for the connection sequence (milliseconds). Not used on HTML5. Default: 3000
desc: Timeout for the connection sequence (milliseconds). Not used on HTML5. (Default is 3000)
- name: protocol
type: string
desc: the protocol to use (e.g. 'chat'). If not set, no `Sec-WebSocket-Protocol` header is sent.
- name: headers
type: string
@@ -55,11 +59,7 @@
- name: message
type: string
desc: The received data. Only valid if event is `websocket.EVENT_MESSAGE`
- name: error
type: string
desc: The error string. Only valid if event is `websocket.EVENT_ERROR`
desc: The received data if event is `websocket.EVENT_MESSAGE`. Error message otherwise
returns:
@@ -79,7 +79,7 @@
update_gui(self)
log("Connected: " .. tostring(conn))
elseif data.event == websocket.EVENT_ERROR then
log("Error: '" .. data.error .. "'")
log("Error: '" .. data.message .. "'")
elseif data.event == websocket.EVENT_MESSAGE then
log("Receiving: '" .. tostring(data.message) .. "'")
end

View File

@@ -40,8 +40,12 @@ static Result SendClientHandshakeHeaders(WebsocketConnection* conn)
dmSnPrintf(port, sizeof(port), ":%d", conn->m_Url.m_Port);
dmSocket::Result sr;
WS_SENDALL("GET /");
WS_SENDALL("GET ");
if (conn->m_Url.m_Path[0] == '\0') {
WS_SENDALL("/"); // Default to / for empty path
} else {
WS_SENDALL(conn->m_Url.m_Path);
}
WS_SENDALL(" HTTP/1.1\r\n");
WS_SENDALL("Host: ");
WS_SENDALL(conn->m_Url.m_Hostname);
@@ -66,6 +70,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");
bail:
@@ -109,17 +119,12 @@ Result ReceiveHeaders(WebsocketConnection* conn)
#else
Result ReceiveHeaders(WebsocketConnection* conn)
{
dmSocket::Selector selector;
dmSocket::SelectorZero(&selector);
dmSocket::SelectorSet(&selector, dmSocket::SELECTOR_KIND_READ, conn->m_Socket);
dmSocket::Result sr = dmSocket::Select(&selector, SOCKET_WAIT_TIMEOUT);
dmSocket::Result sr = WaitForSocket(conn, dmSocket::SELECTOR_KIND_READ, SOCKET_WAIT_TIMEOUT);
if (dmSocket::RESULT_OK != sr)
{
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;
}
@@ -202,9 +207,6 @@ Result VerifyHeaders(WebsocketConnection* conn)
bool connection = false;
bool upgrade = false;
bool valid_key = false;
const char* protocol = "";
// TODO: Perhaps also support the Sec-WebSocket-Protocol
// parse the headers in place
while (r < endtag)

View File

@@ -212,6 +212,7 @@ static void DestroyConnection(WebsocketConnection* conn)
#endif
free((void*)conn->m_CustomHeaders);
free((void*)conn->m_Protocol);
if (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);
lua_Number timeout = dmScript::CheckTableNumber(L, 2, "timeout", 3000);
const char* custom_headers = dmScript::CheckTableString(L, 2, "headers", 0);
const char* protocol = dmScript::CheckTableString(L, 2, "protocol", 0);
if (custom_headers != 0)
{
@@ -283,6 +285,7 @@ static int LuaConnect(lua_State* L)
WebsocketConnection* conn = CreateConnection(url);
conn->m_ConnectTimeout = dmTime::GetTime() + timeout * 1000;
conn->m_CustomHeaders = custom_headers ? strdup(custom_headers) : 0;
conn->m_Protocol = protocol ? strdup(protocol) : 0;
conn->m_Callback = dmScript::CreateCallback(L, 3);
@@ -506,6 +509,12 @@ static dmExtension::Result AppFinalize(dmExtension::AppParams* params)
static dmExtension::Result Finalize(dmExtension::Params* params)
{
while (!g_Websocket.m_Connections.Empty())
{
WebsocketConnection* conn = g_Websocket.m_Connections.Back();
g_Websocket.m_Connections.Pop();
DestroyConnection(conn);
}
return dmExtension::RESULT_OK;
}
@@ -709,6 +718,11 @@ static dmExtension::Result OnUpdate(dmExtension::Params* params)
#if defined(__EMSCRIPTEN__)
conn->m_SSLSocket = dmSSLSocket::INVALID_SOCKET_HANDLE;
EM_ASM({
// https://emscripten.org/docs/porting/networking.html#emulated-posix-tcp-sockets-over-websockets
Module["websocket"]["subprotocol"] = $0 ? UTF8ToString($0) : null;
}, conn->m_Protocol);
char uri_buffer[dmURI::MAX_URI_LEN];
const char* uri;
if (conn->m_Url.m_Path[0] != '\0') {

View File

@@ -91,6 +91,7 @@ namespace dmWebsocket
dmArray<Message> m_Messages; // lengths of the messages in the data buffer
uint64_t m_ConnectTimeout;
uint8_t m_Key[16];
const char* m_Protocol;
const char* m_CustomHeaders;
State m_State;
char* m_Buffer;