Skip to content

Commit e27289d

Browse files
committed
Added a TLS client and a node example to complement
1 parent 8a2243a commit e27289d

11 files changed

+434
-1
lines changed

.vscode/settings.json

+55-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,60 @@
1111
"queue": "cpp",
1212
"xstring": "cpp",
1313
"iostream": "cpp",
14-
"mutex": "cpp"
14+
"mutex": "cpp",
15+
"algorithm": "cpp",
16+
"array": "cpp",
17+
"atomic": "cpp",
18+
"chrono": "cpp",
19+
"cmath": "cpp",
20+
"cstddef": "cpp",
21+
"cstdint": "cpp",
22+
"cstdio": "cpp",
23+
"cstdlib": "cpp",
24+
"cstring": "cpp",
25+
"ctime": "cpp",
26+
"cwchar": "cpp",
27+
"deque": "cpp",
28+
"exception": "cpp",
29+
"functional": "cpp",
30+
"initializer_list": "cpp",
31+
"iomanip": "cpp",
32+
"ios": "cpp",
33+
"iosfwd": "cpp",
34+
"istream": "cpp",
35+
"iterator": "cpp",
36+
"limits": "cpp",
37+
"list": "cpp",
38+
"locale": "cpp",
39+
"map": "cpp",
40+
"new": "cpp",
41+
"ostream": "cpp",
42+
"ratio": "cpp",
43+
"stdexcept": "cpp",
44+
"streambuf": "cpp",
45+
"string": "cpp",
46+
"thread": "cpp",
47+
"tuple": "cpp",
48+
"type_traits": "cpp",
49+
"typeinfo": "cpp",
50+
"unordered_map": "cpp",
51+
"unordered_set": "cpp",
52+
"utility": "cpp",
53+
"vector": "cpp",
54+
"xfacet": "cpp",
55+
"xfunctional": "cpp",
56+
"xhash": "cpp",
57+
"xiosbase": "cpp",
58+
"xlocbuf": "cpp",
59+
"xlocinfo": "cpp",
60+
"xlocmon": "cpp",
61+
"xlocnum": "cpp",
62+
"xloctime": "cpp",
63+
"xmemory": "cpp",
64+
"xmemory0": "cpp",
65+
"xstddef": "cpp",
66+
"xtr1common": "cpp",
67+
"xtree": "cpp",
68+
"xutility": "cpp"
1569
}
1670
}

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_library(ws MODULE
1212
src/lua_ws_client.cpp
1313
src/lua_ws_server.cpp
1414
src/lua_ws_server_channel.cpp
15+
src/lua_wss_client.cpp
1516
src/lua_ws.cpp
1617
)
1718

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,24 @@ For example:
6767
local client = ws.newClient("localhost:8080/game")
6868
```
6969

70+
7071
Creates a client than will connect to localhost (the local machine) at port 8080 on the "game"
7172
namespace.
7273

74+
If you need to connect using TLS for security reasons,
75+
you can also create a secure version of the client socket.
76+
77+
```lua
78+
local client = ws.newTlsClient("localhost:8080/game", true)
79+
```
80+
81+
The second argument (false by default) is if you should validate
82+
the certificate of the server.
83+
84+
On development you may want to use self signed certificates,
85+
if that's the case, put it to false, However, you should
86+
always put it as true in production.
87+
7388
Then you should start the server:
7489

7590
```lua

examples/wss_client.lua

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
local ws = require("ws")
2+
3+
local client = ws.newTlsClient("localhost:8080/test", false)
4+
5+
client:connect()
6+
7+
local connected = true
8+
9+
while connected do
10+
local ev = client:checkQueue()
11+
if ev then
12+
print(ev.type .. ": " .. ev.message)
13+
if ev.type == "error" or ev.type == "close" then
14+
connected = false
15+
end
16+
if ev.type == "open" then
17+
client:sendMessage("hello")
18+
end
19+
end
20+
end
21+
22+
print("Connection closed")

examples/wss_server/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
keys

examples/wss_server/index.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const https = require('https');
2+
const fs = require('fs');
3+
const path = require("path");
4+
5+
const WebSocket = require('ws');
6+
7+
// Remember to generate your keys and put it inside a keys folder.
8+
// For this example, they can be self silgned.
9+
// As an example, you may use this using OpenSSL:
10+
// openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout private.pem -out public.pem
11+
const server = https.createServer({
12+
cert: fs.readFileSync(path.join(__dirname, 'keys', 'public.pem')),
13+
key: fs.readFileSync(path.join(__dirname, 'keys', 'private.pem'))
14+
});
15+
16+
const wss = new WebSocket.Server({ server });
17+
18+
wss.on('connection', function connection (ws) {
19+
console.log("User connected");
20+
ws.on('message', function message (msg) {
21+
console.log(`Message from user: ${msg}.`);
22+
});
23+
});
24+
25+
server.listen(8080);

examples/wss_server/package-lock.json

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/wss_server/package.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "wss_server",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"ws": "^5.1.1"
14+
}
15+
}

include/lua_wss_client.hpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifndef LUA_WSS_CLIENT
2+
#define LUA_WSS_CLIENT
3+
4+
#include <queue>
5+
#include <mutex>
6+
7+
#include "lua.hpp"
8+
#include "client_wss.hpp"
9+
10+
class WssClientMessage {
11+
public:
12+
int type;
13+
std::string message;
14+
WssClientMessage(int type, std::string message);
15+
std::string getTypeName();
16+
};
17+
18+
class WssClient : public SimpleWeb::SocketClient<SimpleWeb::WSS>
19+
{
20+
public:
21+
WssClient(const std::string &server_port_path, bool certificate) noexcept;
22+
bool _serverOpen;
23+
std::mutex _queueMutex;
24+
std::queue<WssClientMessage> _messageQueue;
25+
std::shared_ptr<std::thread> thread;
26+
void popMessage(lua_State *L);
27+
void pushMessage(int type, std::string message);
28+
void sendMessage(std::string message);
29+
};
30+
31+
class LuaWssClient
32+
{
33+
public:
34+
static const char className[];
35+
static const luaL_reg methods[];
36+
// Lua VM functions
37+
static WssClient *check(lua_State *L, int narg);
38+
static void setup(lua_State *L);
39+
static int create(lua_State *L);
40+
static int gc(lua_State *L);
41+
// Lua Methods
42+
static int connect(lua_State *L);
43+
static int close(lua_State *L);
44+
static int checkQueue(lua_State *L);
45+
static int sendMessage(lua_State *L);
46+
static int isOpen(lua_State *L);
47+
};
48+
49+
#endif

src/lua_ws.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "lua.hpp"
22
#include "lua_ws_client.hpp"
3+
#include "lua_wss_client.hpp"
34
#include "lua_ws_server.hpp"
45
#include "lua_ws_server_channel.hpp"
56

@@ -13,8 +14,11 @@ extern "C" LUALIB_API LOVE_WS_EXPORT int
1314
luaopen_ws(lua_State *L)
1415
{
1516
lua_newtable(L);
17+
// Plain websockets
1618
LuaWsClient::setup(L);
1719
LuaWsServer::setup(L);
1820
LuaWsServerChannel::setup(L);
21+
// Secure websockets
22+
LuaWssClient::setup(L);
1923
return 1;
2024
}

0 commit comments

Comments
 (0)