can receive string message

This commit is contained in:
Benjamin Sergeant 2020-04-28 22:15:43 -07:00
parent feff2e38c0
commit 36fbdd0daa
3 changed files with 195 additions and 92 deletions

View File

@ -1,3 +1,9 @@
/*
* LuaWebSocket.h
* Author: Benjamin Sergeant
* Copyright (c) 2020 Machine Zone, Inc. All rights reserved.
*/
#pragma once #pragma once
#include <iostream> #include <iostream>
@ -13,54 +19,116 @@ extern "C"
#include <ixwebsocket/IXWebSocket.h> #include <ixwebsocket/IXWebSocket.h>
#include <chrono> #include <chrono>
#include <thread> #include <thread>
#include <queue>
#include <mutex>
using namespace ix; namespace ix
WebSocket* WebSocket_new(lua_State* L)
{ {
WebSocket* webSocket = new WebSocket(); class LuaWebSocket
webSocket->setOnMessageCallback([](const WebSocketMessagePtr& msg) { {
public:
LuaWebSocket()
{
_webSocket.setOnMessageCallback([this](const WebSocketMessagePtr& msg) {
if (msg->type == ix::WebSocketMessageType::Message) if (msg->type == ix::WebSocketMessageType::Message)
{ {
std::cout << msg->str << std::endl; std::lock_guard<std::mutex> lock(_queueMutex);
_queue.push(msg->str);
} }
}); });
}
void setUrl(const std::string& url) { _webSocket.setUrl(url); }
const std::string& getUrl() { return _webSocket.getUrl(); }
void start() { _webSocket.start(); }
void send(const std::string& msg) { _webSocket.send(msg); }
const std::string& getMessage()
{
std::lock_guard<std::mutex> lock(_queueMutex);
return _queue.front();
}
bool hasMessage()
{
std::lock_guard<std::mutex> lock(_queueMutex);
return !_queue.empty();
}
void popMessage()
{
std::lock_guard<std::mutex> lock(_queueMutex);
_queue.pop();
}
private:
WebSocket _webSocket;
std::queue<std::string> _queue;
std::mutex _queueMutex;
};
LuaWebSocket* WebSocket_new(lua_State* L)
{
LuaWebSocket* webSocket = new LuaWebSocket();
return webSocket; return webSocket;
} }
int WebSocket_getUrl(lua_State* L) int WebSocket_getUrl(lua_State* L)
{ {
WebSocket* webSocket = luaW_check<WebSocket>(L, 1); LuaWebSocket* luaWebSocket = luaW_check<LuaWebSocket>(L, 1);
lua_pushstring(L, webSocket->getUrl().c_str()); lua_pushstring(L, luaWebSocket->getUrl().c_str());
return 1; return 1;
} }
int WebSocket_setUrl(lua_State* L) int WebSocket_setUrl(lua_State* L)
{ {
WebSocket* webSocket = luaW_check<WebSocket>(L, 1); LuaWebSocket* luaWebSocket = luaW_check<LuaWebSocket>(L, 1);
const char* url = luaL_checkstring(L, 2); const char* url = luaL_checkstring(L, 2);
webSocket->setUrl(url); luaWebSocket->setUrl(url);
return 0; return 0;
} }
int WebSocket_start(lua_State* L) int WebSocket_start(lua_State* L)
{ {
WebSocket* webSocket = luaW_check<WebSocket>(L, 1); LuaWebSocket* luaWebSocket = luaW_check<LuaWebSocket>(L, 1);
webSocket->start(); luaWebSocket->start();
return 0; return 0;
} }
int WebSocket_send(lua_State* L) int WebSocket_send(lua_State* L)
{ {
WebSocket* webSocket = luaW_check<WebSocket>(L, 1); LuaWebSocket* luaWebSocket = luaW_check<LuaWebSocket>(L, 1);
const char* msg = luaL_checkstring(L, 2); const char* msg = luaL_checkstring(L, 2);
webSocket->send(msg); luaWebSocket->send(msg);
return 0; return 0;
} }
int WebSocket_getMessage(lua_State* L)
{
LuaWebSocket* luaWebSocket = luaW_check<LuaWebSocket>(L, 1);
lua_pushstring(L, luaWebSocket->getMessage().c_str());
return 1;
}
int WebSocket_hasMessage(lua_State* L)
{
LuaWebSocket* luaWebSocket = luaW_check<LuaWebSocket>(L, 1);
lua_pushboolean(L, luaWebSocket->hasMessage());
return 1;
}
int WebSocket_popMessage(lua_State* L)
{
LuaWebSocket* luaWebSocket = luaW_check<LuaWebSocket>(L, 1);
luaWebSocket->popMessage();
return 1;
}
// This should be a static method
int WebSocket_sleep(lua_State* L) int WebSocket_sleep(lua_State* L)
{ {
WebSocket* webSocket = luaW_check<WebSocket>(L, 1); LuaWebSocket* luaWebSocket = luaW_check<LuaWebSocket>(L, 1);
int duration = luaL_checkinteger(L, 2); int duration = luaL_checkinteger(L, 2);
std::this_thread::sleep_for(std::chrono::milliseconds(duration)); std::this_thread::sleep_for(std::chrono::milliseconds(duration));
return 0; return 0;
@ -75,13 +143,16 @@ static luaL_Reg WebSocket_metatable[] = {
{ "setUrl", WebSocket_setUrl }, { "setUrl", WebSocket_setUrl },
{ "start", WebSocket_start }, { "start", WebSocket_start },
{ "send", WebSocket_send }, { "send", WebSocket_send },
{ "getMessage", WebSocket_getMessage },
{ "hasMessage", WebSocket_hasMessage },
{ "popMessage", WebSocket_popMessage },
{ "sleep", WebSocket_sleep }, { "sleep", WebSocket_sleep },
{ NULL, NULL } { NULL, NULL }
}; };
int luaopen_WebSocket(lua_State* L) int luaopen_WebSocket(lua_State* L)
{ {
luaW_register<WebSocket>(L, luaW_register<LuaWebSocket>(L,
"WebSocket", "WebSocket",
WebSocket_table, WebSocket_table,
WebSocket_metatable, WebSocket_metatable,
@ -89,3 +160,4 @@ int luaopen_WebSocket(lua_State* L)
); );
return 1; return 1;
} }
}

View File

@ -1,13 +1,28 @@
webSocket = WebSocket.new() --
-- make luarocks && (cd luarocks ; ../build/luarocks/luarocks)
--
local webSocket = WebSocket.new()
webSocket:setUrl("ws://localhost:8008") webSocket:setUrl("ws://localhost:8008")
print("Url: " .. webSocket:getUrl()) print("Url: " .. webSocket:getUrl())
-- Start the background thread
webSocket:start() webSocket:start()
local i = 0
while true while true
do do
print("Waiting ...") print("Sending message...")
webSocket:send("coucou"); webSocket:send("msg_" .. tostring(i));
i = i + 1
print("Waiting 1s...")
webSocket:sleep(1000) webSocket:sleep(1000)
if webSocket:hasMessage() then
local msg = webSocket:getMessage()
print("Received: " .. msg)
webSocket:popMessage()
end
end end

View File

@ -1,3 +1,9 @@
/*
* main.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2020 Machine Zone, Inc. All rights reserved.
*/
#include <iostream> #include <iostream>
extern "C" extern "C"
@ -20,29 +26,39 @@ int main()
lua_register(L, "info", lua_info); lua_register(L, "info", lua_info);
// Objets // Objets
luaopen_WebSocket(L); ix::luaopen_WebSocket(L);
#if 0 //
luaL_dofile(L, "ia.lua"); // Simple version does little error handling
#else // luaL_dofile(L, "ia.lua");
int loadStatus = luaL_loadfile(L, "ia.lua"); //
std::string luaFile("ia.lua");
int loadStatus = luaL_loadfile(L, luaFile.c_str());
if (loadStatus) if (loadStatus)
{ {
std::cerr << "Error loading gof_server.lua" << std::endl; std::cerr << "Error loading " << luaFile << std::endl;
std::cerr << lua_tostring(L, -1) << std::endl; std::cerr << lua_tostring(L, -1) << std::endl;
return 1; return 1;
} }
else else
{ {
std::cout << "loaded ia.lua" << std::endl; std::cout << "loaded " << luaFile << std::endl;
} }
try { //
// Capture lua errors
//
try
{
lua_call(L, 0, 0); lua_call(L, 0, 0);
}
catch (const std::runtime_error& ex) {
std::cerr << ex.what() << std::endl;
}
#endif
lua_close(L); lua_close(L);
} }
catch (const std::runtime_error& ex)
{
lua_close(L);
std::cerr << ex.what() << std::endl;
return 1;
}
return 0;
}