Version 1.4 - Websockets and bug fixes

This commit is contained in:
OTCv8
2019-11-26 02:32:51 +01:00
parent 1072671986
commit c750ea65f8
29 changed files with 426 additions and 124 deletions

View File

@@ -96,24 +96,32 @@ end)
### Regex
If you're pro, there's also support for simple regex in lua which look like this:
```
g_lua.bindGlobalFunction("regexMatch", [](std::string s, const std::string& exp) {
int limit = 10000;
std::vector<std::vector<std::string>> ret;
if (s.empty() || exp.empty())
return ret;
try {
std::smatch m;
std::regex e(exp);
while (std::regex_search (s,m,e)) {
ret.push_back(std::vector<std::string>());
for (auto x:m)
ret[ret.size() - 1].push_back(x);
s = m.suffix().str();
if (--limit == 0)
return ret;
}
} catch (...) {
}
return ret;
});
let clients = new Set();
require('uWebSockets.js').App().ws('/*', {
open: (ws, req) => {
clients.add(ws);
},
message: (ws, message, isBinary) => {
console.log("Message: " + message);
let ok = ws.send(message, isBinary);
},
close: (ws, code, message) => {
clients.delete(ws);
}
}).any('/*', (res, req) => {
/* Let's deny all Http */
res.end('Nothing to see here!');
}).listen(9000, (listenSocket) => {
if (listenSocket) {
console.log('Listening to port 9000');
}
});
setInterval(() => {
for(let client of clients) {
client.send("hello", false);
}
}, 1000);
```

91
tutorials/Websockets.md Normal file
View File

@@ -0,0 +1,91 @@
## OTClientV8 Websockets
From version 1.4 OTClientV8 supports websockets and secure websockets. They can be also used in bot.
### Usage
```
local url = "ws://otclient.ovh/"
local websocket = HTTP.WebSocket(url, {
onOpen = function(message, websocketId)
end,
onMessage = function(message, websocketId)
end,
onClose = function(message, websocketId)
end,
onError = function(message, websocketId)
end
})
-- it returns
print(websocket.id)
print(websocket.url)
websocket.send("Hello")
scheduleEvent(function()
websocket.close()
end, 5000)
```
If your websocket is only using json then you can use HTTP.WebSocketJSON
```
local url = "wss://otclient.ovh:3000/"
local websocket = HTTP.WebSocketJSON(url, {
onOpen = function(message, websocketId)
end,
onMessage = function(message, websocketId)
-- message is table, after json.decode
end,
onClose = function(message, websocketId)
end,
onError = function(message, websocketId)
-- will also return json errors
end
})
-- it returns
print(websocket.id)
print(websocket.url)
websocket.send({message="Hello"})
scheduleEvent(function()
websocket.close()
end, 5000)
```
A working example with reconnect can be found in `client_entergame/entergame.lua`
### Websockets have 15s timeout by default, you can change it in `corelib/http.lua`
### WebSocket server
Creating websocket server is easy, here are some links:
https://github.com/websockets/ws
https://medium.com/@martin.sikora/node-js-websocket-simple-chat-tutorial-2def3a841b61
https://medium.com/hackernoon/implementing-a-websocket-server-with-node-js-d9b78ec5ffa8
Personally, I use:
https://github.com/uNetworking/uWebSockets
https://github.com/uNetworking/uWebSockets.js
### Example server in nodejs
You need to install nodejs and then `npm install uNetworking/uWebSockets.js#v16.4.0`
Name it server.js and run it by using command: `nodejs server.js`
```
require('uWebSockets.js').App().ws('/*', {
message: (ws, message, isBinary) => {
console.log("message");
let ok = ws.send(message, isBinary);
}
}).any('/*', (res, req) => {
/* Let's deny all Http */
res.end('Nothing to see here!');
}).listen(9000, (listenSocket) => {
if (listenSocket) {
console.log('Listening to port 9000');
}
});
```
More examples: https://github.com/uNetworking/uWebSockets.js/tree/master/examples