mirror of
https://github.com/defold/extension-websocket.git
synced 2025-06-27 09:47:44 +02:00
Issue 11: Added support for both binary and text frames
This commit is contained in:
parent
ba2b8e4a69
commit
9d1ace0a82
@ -113,6 +113,17 @@
|
|||||||
- name: message
|
- name: message
|
||||||
type: string
|
type: string
|
||||||
desc: the message to send
|
desc: the message to send
|
||||||
|
- name: options
|
||||||
|
type: table
|
||||||
|
desc: options for this particular message. May be `nil`
|
||||||
|
members:
|
||||||
|
- name: type
|
||||||
|
type: number
|
||||||
|
desc: The data type of the message
|
||||||
|
|
||||||
|
- `websocket.DATA_TYPE_BINARY` (default)
|
||||||
|
|
||||||
|
- `websocket.DATA_TYPE_TEXT`
|
||||||
|
|
||||||
examples:
|
examples:
|
||||||
- desc: |-
|
- desc: |-
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
#include "script_util.h"
|
#include "script_util.h"
|
||||||
|
|
||||||
namespace dmWebsocket {
|
namespace dmScript {
|
||||||
|
|
||||||
bool luaL_checkbool(lua_State *L, int numArg)
|
bool CheckBool(lua_State *L, int numArg)
|
||||||
{
|
{
|
||||||
bool b = false;
|
bool b = false;
|
||||||
if (lua_isboolean(L, numArg))
|
if (lua_isboolean(L, numArg))
|
||||||
@ -16,17 +16,17 @@ bool luaL_checkbool(lua_State *L, int numArg)
|
|||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool luaL_checkboold(lua_State *L, int numArg, int def)
|
bool CheckBoold(lua_State *L, int numArg, int def)
|
||||||
{
|
{
|
||||||
int type = lua_type(L, numArg);
|
int type = lua_type(L, numArg);
|
||||||
if (type != LUA_TNONE && type != LUA_TNIL)
|
if (type != LUA_TNONE && type != LUA_TNIL)
|
||||||
{
|
{
|
||||||
return luaL_checkbool(L, numArg);
|
return CheckBool(L, numArg);
|
||||||
}
|
}
|
||||||
return def;
|
return def;
|
||||||
}
|
}
|
||||||
|
|
||||||
lua_Number luaL_checknumberd(lua_State *L, int numArg, lua_Number def)
|
lua_Number CheckNumberd(lua_State *L, int numArg, lua_Number def)
|
||||||
{
|
{
|
||||||
int type = lua_type(L, numArg);
|
int type = lua_type(L, numArg);
|
||||||
if (type != LUA_TNONE && type != LUA_TNIL)
|
if (type != LUA_TNONE && type != LUA_TNIL)
|
||||||
@ -36,7 +36,7 @@ lua_Number luaL_checknumberd(lua_State *L, int numArg, lua_Number def)
|
|||||||
return def;
|
return def;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* luaL_checkstringd(lua_State *L, int numArg, const char* def)
|
char* CheckStringd(lua_State *L, int numArg, const char* def)
|
||||||
{
|
{
|
||||||
int type = lua_type(L, numArg);
|
int type = lua_type(L, numArg);
|
||||||
if (type != LUA_TNONE && type != LUA_TNIL)
|
if (type != LUA_TNONE && type != LUA_TNIL)
|
||||||
@ -46,7 +46,7 @@ char* luaL_checkstringd(lua_State *L, int numArg, const char* def)
|
|||||||
return (char*)def;
|
return (char*)def;
|
||||||
}
|
}
|
||||||
|
|
||||||
lua_Number luaL_checktable_number(lua_State *L, int numArg, const char* field, lua_Number def)
|
lua_Number CheckTableNumber(lua_State *L, int numArg, const char* field, lua_Number def)
|
||||||
{
|
{
|
||||||
lua_Number result = def;
|
lua_Number result = def;
|
||||||
if(lua_istable(L, numArg))
|
if(lua_istable(L, numArg))
|
||||||
@ -61,7 +61,7 @@ lua_Number luaL_checktable_number(lua_State *L, int numArg, const char* field, l
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* luaL_checktable_string(lua_State *L, int numArg, const char* field, char* def)
|
char* CheckTableString(lua_State *L, int numArg, const char* field, char* def)
|
||||||
{
|
{
|
||||||
char* result = def;
|
char* result = def;
|
||||||
if(lua_istable(L, numArg))
|
if(lua_istable(L, numArg))
|
||||||
|
@ -2,11 +2,11 @@
|
|||||||
|
|
||||||
#include <dmsdk/sdk.h>
|
#include <dmsdk/sdk.h>
|
||||||
|
|
||||||
namespace dmWebsocket {
|
namespace dmScript {
|
||||||
bool luaL_checkbool(lua_State *L, int numArg);
|
bool CheckBool(lua_State *L, int numArg);
|
||||||
bool luaL_checkboold(lua_State *L, int numArg, int def);
|
bool CheckBoold(lua_State *L, int numArg, int def);
|
||||||
lua_Number luaL_checknumberd(lua_State *L, int numArg, lua_Number def);
|
lua_Number CheckNumberd(lua_State *L, int numArg, lua_Number def);
|
||||||
char* luaL_checkstringd(lua_State *L, int numArg, const char* def);
|
char* CheckStringd(lua_State *L, int numArg, const char* def);
|
||||||
lua_Number luaL_checktable_number(lua_State *L, int numArg, const char* field, lua_Number def);
|
lua_Number CheckTableNumber(lua_State *L, int numArg, const char* field, lua_Number def);
|
||||||
char* luaL_checktable_string(lua_State *L, int numArg, const char* field, char* def);
|
char* CheckTableString(lua_State *L, int numArg, const char* field, char* def);
|
||||||
} // namespace
|
} // namespace
|
@ -325,7 +325,7 @@ static int LuaSend(lua_State* L)
|
|||||||
const char* string = luaL_checklstring(L, 2, &string_length);
|
const char* string = luaL_checklstring(L, 2, &string_length);
|
||||||
|
|
||||||
#if defined(HAVE_WSLAY)
|
#if defined(HAVE_WSLAY)
|
||||||
int write_mode = WSLAY_BINARY_FRAME; // or WSLAY_TEXT_FRAME
|
int write_mode = dmScript::CheckTableNumber(L, 3, "type", WSLAY_BINARY_FRAME);
|
||||||
|
|
||||||
struct wslay_event_msg msg;
|
struct wslay_event_msg msg;
|
||||||
msg.opcode = write_mode;
|
msg.opcode = write_mode;
|
||||||
@ -403,6 +403,16 @@ static void LuaInit(lua_State* L)
|
|||||||
SETCONSTANT(EVENT_MESSAGE);
|
SETCONSTANT(EVENT_MESSAGE);
|
||||||
SETCONSTANT(EVENT_ERROR);
|
SETCONSTANT(EVENT_ERROR);
|
||||||
|
|
||||||
|
#if defined(HAVE_WSLAY)
|
||||||
|
lua_pushnumber(L, (lua_Number) WSLAY_BINARY_FRAME);
|
||||||
|
lua_setfield(L, -2, "DATA_TYPE_BINARY");
|
||||||
|
lua_pushnumber(L, (lua_Number) WSLAY_TEXT_FRAME);
|
||||||
|
lua_setfield(L, -2, "DATA_TYPE_TEXT");
|
||||||
|
#else
|
||||||
|
SETCONSTANT(DATA_TYPE_BINARY);
|
||||||
|
SETCONSTANT(DATA_TYPE_TEXT);
|
||||||
|
#endif
|
||||||
|
|
||||||
#undef SETCONSTANT
|
#undef SETCONSTANT
|
||||||
|
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
|
@ -66,6 +66,12 @@ namespace dmWebsocket
|
|||||||
MESSAGE_TYPE_CLOSE = 1,
|
MESSAGE_TYPE_CLOSE = 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum DataType
|
||||||
|
{
|
||||||
|
DATA_TYPE_BINARY = 0,
|
||||||
|
DATA_TYPE_TEXT = 1,
|
||||||
|
};
|
||||||
|
|
||||||
struct Message
|
struct Message
|
||||||
{
|
{
|
||||||
uint32_t m_Length:30;
|
uint32_t m_Length:30;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user