can send message, set a url and start the connection
This commit is contained in:
parent
a040ff06e8
commit
feff2e38c0
@ -10,81 +10,82 @@ extern "C"
|
|||||||
}
|
}
|
||||||
|
|
||||||
#include "luawrapper.hpp"
|
#include "luawrapper.hpp"
|
||||||
#include "Player.hpp"
|
#include <ixwebsocket/IXWebSocket.h>
|
||||||
|
#include <chrono>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
Player* Player_new(lua_State* L)
|
using namespace ix;
|
||||||
|
|
||||||
|
WebSocket* WebSocket_new(lua_State* L)
|
||||||
{
|
{
|
||||||
const char* name = luaL_checkstring(L, 1);
|
WebSocket* webSocket = new WebSocket();
|
||||||
unsigned int health = luaL_checkinteger(L, 2);
|
webSocket->setOnMessageCallback([](const WebSocketMessagePtr& msg) {
|
||||||
return new Player(name, health);
|
if (msg->type == ix::WebSocketMessageType::Message)
|
||||||
|
{
|
||||||
|
std::cout << msg->str << std::endl;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return webSocket;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Player_getName(lua_State* L)
|
int WebSocket_getUrl(lua_State* L)
|
||||||
{
|
{
|
||||||
Player* player = luaW_check<Player>(L, 1);
|
WebSocket* webSocket = luaW_check<WebSocket>(L, 1);
|
||||||
lua_pushstring(L, player->getName());
|
lua_pushstring(L, webSocket->getUrl().c_str());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Player_getHealth(lua_State* L)
|
int WebSocket_setUrl(lua_State* L)
|
||||||
{
|
{
|
||||||
Player* player = luaW_check<Player>(L, 1);
|
WebSocket* webSocket = luaW_check<WebSocket>(L, 1);
|
||||||
lua_pushinteger(L, player->getHealth());
|
const char* url = luaL_checkstring(L, 2);
|
||||||
return 1;
|
webSocket->setUrl(url);
|
||||||
}
|
|
||||||
|
|
||||||
int Player_setHealth(lua_State* L)
|
|
||||||
{
|
|
||||||
Player* player = luaW_check<Player>(L, 1);
|
|
||||||
unsigned int health = luaL_checkinteger(L, 2);
|
|
||||||
player->setHealth(health);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Player_heal(lua_State* L)
|
int WebSocket_start(lua_State* L)
|
||||||
{
|
{
|
||||||
Player* player = luaW_check<Player>(L, 1);
|
WebSocket* webSocket = luaW_check<WebSocket>(L, 1);
|
||||||
Player* target = luaW_check<Player>(L, 2);
|
webSocket->start();
|
||||||
player->heal(target);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Player_say(lua_State* L)
|
int WebSocket_send(lua_State* L)
|
||||||
{
|
{
|
||||||
Player* player = luaW_check<Player>(L, 1);
|
WebSocket* webSocket = luaW_check<WebSocket>(L, 1);
|
||||||
const char* text = luaL_checkstring(L, 2);
|
const char* msg = luaL_checkstring(L, 2);
|
||||||
player->say(text);
|
webSocket->send(msg);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Player_info(lua_State* L)
|
int WebSocket_sleep(lua_State* L)
|
||||||
{
|
{
|
||||||
Player* player = luaW_check<Player>(L, 1);
|
WebSocket* webSocket = luaW_check<WebSocket>(L, 1);
|
||||||
player->info();
|
int duration = luaL_checkinteger(L, 2);
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(duration));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static luaL_Reg Player_table[] = {
|
static luaL_Reg WebSocket_table[] = {
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
static luaL_Reg Player_metatable[] = {
|
static luaL_Reg WebSocket_metatable[] = {
|
||||||
{ "getName", Player_getName },
|
{ "getUrl", WebSocket_getUrl },
|
||||||
{ "getHealth", Player_getHealth },
|
{ "setUrl", WebSocket_setUrl },
|
||||||
{ "setHealth", Player_setHealth },
|
{ "start", WebSocket_start },
|
||||||
{ "heal", Player_heal },
|
{ "send", WebSocket_send },
|
||||||
{ "say", Player_say },
|
{ "sleep", WebSocket_sleep },
|
||||||
{ "info", Player_info },
|
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
int luaopen_Player(lua_State* L)
|
int luaopen_WebSocket(lua_State* L)
|
||||||
{
|
{
|
||||||
luaW_register<Player>(L,
|
luaW_register<WebSocket>(L,
|
||||||
"Player",
|
"WebSocket",
|
||||||
Player_table,
|
WebSocket_table,
|
||||||
Player_metatable,
|
WebSocket_metatable,
|
||||||
Player_new
|
WebSocket_new
|
||||||
);
|
);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -1,27 +1,13 @@
|
|||||||
info()
|
webSocket = WebSocket.new()
|
||||||
|
|
||||||
batman = Player.new("Batman", 100)
|
webSocket:setUrl("ws://localhost:8008")
|
||||||
robin = Player.new("Robin", 100)
|
print("Url: " .. webSocket:getUrl())
|
||||||
|
|
||||||
robin:say("I'm the team lea...")
|
webSocket:start()
|
||||||
batman:say("Shut up!\n")
|
|
||||||
|
|
||||||
print(batman:getName().." slap "..robin:getName())
|
while true
|
||||||
math.randomseed(os.time())
|
do
|
||||||
robin:setHealth(math.random(20, 80))
|
print("Waiting ...")
|
||||||
robin:info()
|
webSocket:send("coucou");
|
||||||
|
webSocket:sleep(1000)
|
||||||
print("\n")
|
end
|
||||||
|
|
||||||
print(batman:getName().." heal "..robin:getName())
|
|
||||||
batman:heal(robin)
|
|
||||||
robin:info()
|
|
||||||
|
|
||||||
-- Robin: I'm the team lea...
|
|
||||||
-- Batman: Shut up!
|
|
||||||
--
|
|
||||||
-- Batman slap Robin
|
|
||||||
-- Robin have 80 HP
|
|
||||||
--
|
|
||||||
-- Batman heal Robin
|
|
||||||
-- Robin have 100 HP
|
|
||||||
|
@ -9,6 +9,7 @@ extern "C"
|
|||||||
|
|
||||||
#include "functions.hpp"
|
#include "functions.hpp"
|
||||||
#include "LuaWebSocket.h"
|
#include "LuaWebSocket.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
@ -19,8 +20,29 @@ int main()
|
|||||||
lua_register(L, "info", lua_info);
|
lua_register(L, "info", lua_info);
|
||||||
|
|
||||||
// Objets
|
// Objets
|
||||||
luaopen_Player(L);
|
luaopen_WebSocket(L);
|
||||||
|
|
||||||
|
#if 0
|
||||||
luaL_dofile(L, "ia.lua");
|
luaL_dofile(L, "ia.lua");
|
||||||
|
#else
|
||||||
|
int loadStatus = luaL_loadfile(L, "ia.lua");
|
||||||
|
if (loadStatus)
|
||||||
|
{
|
||||||
|
std::cerr << "Error loading gof_server.lua" << std::endl;
|
||||||
|
std::cerr << lua_tostring(L, -1) << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cout << "loaded ia.lua" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
lua_call(L, 0, 0);
|
||||||
|
}
|
||||||
|
catch (const std::runtime_error& ex) {
|
||||||
|
std::cerr << ex.what() << std::endl;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
lua_close(L);
|
lua_close(L);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user