can receive string message
This commit is contained in:
parent
feff2e38c0
commit
36fbdd0daa
@ -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>
|
||||||
@ -5,87 +11,153 @@
|
|||||||
|
|
||||||
extern "C"
|
extern "C"
|
||||||
{
|
{
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "luawrapper.hpp"
|
#include "luawrapper.hpp"
|
||||||
#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) {
|
{
|
||||||
if (msg->type == ix::WebSocketMessageType::Message)
|
public:
|
||||||
{
|
LuaWebSocket()
|
||||||
std::cout << msg->str << std::endl;
|
{
|
||||||
}
|
_webSocket.setOnMessageCallback([this](const WebSocketMessagePtr& msg) {
|
||||||
});
|
if (msg->type == ix::WebSocketMessageType::Message)
|
||||||
return webSocket;
|
{
|
||||||
}
|
std::lock_guard<std::mutex> lock(_queueMutex);
|
||||||
|
_queue.push(msg->str);
|
||||||
int WebSocket_getUrl(lua_State* L)
|
}
|
||||||
{
|
});
|
||||||
WebSocket* webSocket = luaW_check<WebSocket>(L, 1);
|
}
|
||||||
lua_pushstring(L, webSocket->getUrl().c_str());
|
|
||||||
return 1;
|
void setUrl(const std::string& url) { _webSocket.setUrl(url); }
|
||||||
}
|
const std::string& getUrl() { return _webSocket.getUrl(); }
|
||||||
|
void start() { _webSocket.start(); }
|
||||||
int WebSocket_setUrl(lua_State* L)
|
void send(const std::string& msg) { _webSocket.send(msg); }
|
||||||
{
|
|
||||||
WebSocket* webSocket = luaW_check<WebSocket>(L, 1);
|
const std::string& getMessage()
|
||||||
const char* url = luaL_checkstring(L, 2);
|
{
|
||||||
webSocket->setUrl(url);
|
std::lock_guard<std::mutex> lock(_queueMutex);
|
||||||
return 0;
|
return _queue.front();
|
||||||
}
|
}
|
||||||
|
|
||||||
int WebSocket_start(lua_State* L)
|
bool hasMessage()
|
||||||
{
|
{
|
||||||
WebSocket* webSocket = luaW_check<WebSocket>(L, 1);
|
std::lock_guard<std::mutex> lock(_queueMutex);
|
||||||
webSocket->start();
|
return !_queue.empty();
|
||||||
return 0;
|
}
|
||||||
}
|
|
||||||
|
void popMessage()
|
||||||
int WebSocket_send(lua_State* L)
|
{
|
||||||
{
|
std::lock_guard<std::mutex> lock(_queueMutex);
|
||||||
WebSocket* webSocket = luaW_check<WebSocket>(L, 1);
|
_queue.pop();
|
||||||
const char* msg = luaL_checkstring(L, 2);
|
}
|
||||||
webSocket->send(msg);
|
|
||||||
return 0;
|
private:
|
||||||
}
|
WebSocket _webSocket;
|
||||||
|
|
||||||
int WebSocket_sleep(lua_State* L)
|
std::queue<std::string> _queue;
|
||||||
{
|
std::mutex _queueMutex;
|
||||||
WebSocket* webSocket = luaW_check<WebSocket>(L, 1);
|
};
|
||||||
int duration = luaL_checkinteger(L, 2);
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(duration));
|
LuaWebSocket* WebSocket_new(lua_State* L)
|
||||||
return 0;
|
{
|
||||||
}
|
LuaWebSocket* webSocket = new LuaWebSocket();
|
||||||
|
return webSocket;
|
||||||
static luaL_Reg WebSocket_table[] = {
|
}
|
||||||
{ NULL, NULL }
|
|
||||||
};
|
int WebSocket_getUrl(lua_State* L)
|
||||||
|
{
|
||||||
static luaL_Reg WebSocket_metatable[] = {
|
LuaWebSocket* luaWebSocket = luaW_check<LuaWebSocket>(L, 1);
|
||||||
{ "getUrl", WebSocket_getUrl },
|
lua_pushstring(L, luaWebSocket->getUrl().c_str());
|
||||||
{ "setUrl", WebSocket_setUrl },
|
return 1;
|
||||||
{ "start", WebSocket_start },
|
}
|
||||||
{ "send", WebSocket_send },
|
|
||||||
{ "sleep", WebSocket_sleep },
|
int WebSocket_setUrl(lua_State* L)
|
||||||
{ NULL, NULL }
|
{
|
||||||
};
|
LuaWebSocket* luaWebSocket = luaW_check<LuaWebSocket>(L, 1);
|
||||||
|
const char* url = luaL_checkstring(L, 2);
|
||||||
int luaopen_WebSocket(lua_State* L)
|
luaWebSocket->setUrl(url);
|
||||||
{
|
return 0;
|
||||||
luaW_register<WebSocket>(L,
|
}
|
||||||
"WebSocket",
|
|
||||||
WebSocket_table,
|
int WebSocket_start(lua_State* L)
|
||||||
WebSocket_metatable,
|
{
|
||||||
WebSocket_new
|
LuaWebSocket* luaWebSocket = luaW_check<LuaWebSocket>(L, 1);
|
||||||
);
|
luaWebSocket->start();
|
||||||
return 1;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int WebSocket_send(lua_State* L)
|
||||||
|
{
|
||||||
|
LuaWebSocket* luaWebSocket = luaW_check<LuaWebSocket>(L, 1);
|
||||||
|
const char* msg = luaL_checkstring(L, 2);
|
||||||
|
luaWebSocket->send(msg);
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
LuaWebSocket* luaWebSocket = luaW_check<LuaWebSocket>(L, 1);
|
||||||
|
int duration = luaL_checkinteger(L, 2);
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(duration));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static luaL_Reg WebSocket_table[] = {
|
||||||
|
{ NULL, NULL }
|
||||||
|
};
|
||||||
|
|
||||||
|
static luaL_Reg WebSocket_metatable[] = {
|
||||||
|
{ "getUrl", WebSocket_getUrl },
|
||||||
|
{ "setUrl", WebSocket_setUrl },
|
||||||
|
{ "start", WebSocket_start },
|
||||||
|
{ "send", WebSocket_send },
|
||||||
|
{ "getMessage", WebSocket_getMessage },
|
||||||
|
{ "hasMessage", WebSocket_hasMessage },
|
||||||
|
{ "popMessage", WebSocket_popMessage },
|
||||||
|
{ "sleep", WebSocket_sleep },
|
||||||
|
{ NULL, NULL }
|
||||||
|
};
|
||||||
|
|
||||||
|
int luaopen_WebSocket(lua_State* L)
|
||||||
|
{
|
||||||
|
luaW_register<LuaWebSocket>(L,
|
||||||
|
"WebSocket",
|
||||||
|
WebSocket_table,
|
||||||
|
WebSocket_metatable,
|
||||||
|
WebSocket_new
|
||||||
|
);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
@ -1,10 +1,16 @@
|
|||||||
|
/*
|
||||||
|
* main.cpp
|
||||||
|
* Author: Benjamin Sergeant
|
||||||
|
* Copyright (c) 2020 Machine Zone, Inc. All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
extern "C"
|
extern "C"
|
||||||
{
|
{
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
#include "lualib.h"
|
#include "lualib.h"
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "functions.hpp"
|
#include "functions.hpp"
|
||||||
@ -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);
|
||||||
|
lua_close(L);
|
||||||
}
|
}
|
||||||
catch (const std::runtime_error& ex) {
|
catch (const std::runtime_error& ex)
|
||||||
|
{
|
||||||
|
lua_close(L);
|
||||||
std::cerr << ex.what() << std::endl;
|
std::cerr << ex.what() << std::endl;
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
lua_close(L);
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user