It would be really useful if it was possible to use Websockets for communicating with the 'outside world'.
That way WeBuilder could send/receive data between editor and i.e. nodejs scripts, without having to setup hidden Webkit frames.
Support for Websockets in plugins
Moderator: kfury77
Forum rules
Please follow these guidelines when posting feature requests. This will help to increase the value of your contribution.
Please follow these guidelines when posting feature requests. This will help to increase the value of your contribution.
- Do not create new topics for already requested features. Add your comments to the existing feature request topics instead;
- Create separate topic for each feature suggestion. Do NOT post a number of non-related feature suggestions in a single topic;
- Give your topic a meaningful title. Do NOT create topics with meaningless titles, such as "My Suggestion" or "My Problem".
Support for Websockets in plugins
There are 10 types of people in the world: Those who understand binary and those who don't.
Re: Support for Websockets in plugins
It would be very similar to how the Webkit/Chromium frame works, and would only require 4 signals/events.
You start by listening for an url/port (similar to how Webkit frame listens for channels)
Signals/Events needed.
1) "onWebsocketOpen" - fired when you make the initial connection and starts listening.
2) "onWebsocketMessage" - fired when data is recieved from the websocket.
3) "onWebsocketClose" - fired when connection is closed.
4) "onWebsocketError" - fired when an error occurs.
You start by listening for an url/port (similar to how Webkit frame listens for channels)
Signals/Events needed.
1) "onWebsocketOpen" - fired when you make the initial connection and starts listening.
2) "onWebsocketMessage" - fired when data is recieved from the websocket.
3) "onWebsocketClose" - fired when connection is closed.
4) "onWebsocketError" - fired when an error occurs.
There are 10 types of people in the world: Those who understand binary and those who don't.
Re: Support for Websockets in plugins
Is this like a built-in on-demand server? Are there any security implications?
Blumentals Software Programmer
Re: Support for Websockets in plugins
Websockets is just a communications protocol. It supports CORS.
In JavaScript, you setup a WebSocket CLIENT, like this:
This allows bidirectional communication between the client and a (node.js) Push server located at "localhost:7913"
The server can then process data send using the "send()" method. And return the result asynchronously, triggering the "onmessage" siggnal at the client.
Some useful links for more information.
http://codular.com/node-web-sockets
https://www.html5rocks.com/en/tutorials ... ts/basics/
https://www.tutorialspoint.com/websockets/index.htm
Node.js WebSocket library:
https://github.com/websockets/ws
In JavaScript, you setup a WebSocket CLIENT, like this:
Code: Select all
//create a new WebSocket object.
var websocket = new WebSocket("ws://localhost:7913");
// Events/Signals
websocket.onopen = function(evt) { /* do stuff */ }; //on open event
websocket.onclose = function(evt) { /* do stuff */ }; //on close event
websocket.onmessage = function(evt) { /* do stuff */ }; //on message event
websocket.onerror = function(evt) { /* do stuff */ }; //on error event
// Methods
websocket.send(message); //send method
websocket.close(); //close method
The server can then process data send using the "send()" method. And return the result asynchronously, triggering the "onmessage" siggnal at the client.
Some useful links for more information.
http://codular.com/node-web-sockets
https://www.html5rocks.com/en/tutorials ... ts/basics/
https://www.tutorialspoint.com/websockets/index.htm
Node.js WebSocket library:
https://github.com/websockets/ws
There are 10 types of people in the world: Those who understand binary and those who don't.
Re: Support for Websockets in plugins
*Bump*
There are 10 types of people in the world: Those who understand binary and those who don't.
Re: Support for Websockets in plugins
Alright, I looked for a native solutions and there aren't any free libraries for this, but you can proxy it through Chromium JavaScript engine and it's easier to do in the upcoming version. Let me demonstrate for future reference.
JScript, script.js:
JavaScript, websockets.js
JScript, script.js:
Code: Select all
var js_executer_websockets = null;
var script_loaded = false;
var test_counter = 0;
//this is just for debugging, otherwise not required
function OnWebkitConsoleMessage(Sender, browser, message, source, line, Res) {
alert(_t(line) + ":" + source + ":" + message);
}
function WebsocketsCallback(res, err) {
var json = new TScriptableJSON();
json.Parse(res);
var cmd = json.GetValue("cmd");
var message = "";
if ((cmd == "message") || (cmd == "error")) {
message = json.GetValue("message");
}
Script.Message("Websockets event: " + cmd + ": " + message);
delete json;
}
function TestJSExecuterWebsockets(Sender) {
if (scriptexec == null) {
scriptexec = Script.CreateScriptableJsExecuter("");
scriptexec.OnConsoleMessage = &OnWebkitConsoleMessage; //just for debugging
}
var script_file = Script.Path + "websockets.js";
if (!script_loaded) {
scriptexec.ExecuteJavaScriptFileRequest(script_file, "", "", &WebsocketsCallback) //load script
script_loaded = true;
}
//sending will probably fail on the 1st call because websockets won't be conected yet; it's asynchronous and takes time
//the script could be improved by moving the 1st sending attempt to WebsocketsCallback on receiving "open"
scriptexec.ExecuteJavaScriptFileRequest("", "", "Test Message To Send " + _T(test_counter), &WebsocketsCallback)
test_counter = test_counter + 1;
}
Script.RegisterAction("Test", "Test Websockets", "", &TestJSExecuterWebsockets);
JavaScript, websockets.js
Code: Select all
var channel;
//create a new WebSocket object.
//we do it only once when script is loaded for this test script
var websocket = new WebSocket("ws://echo.websocket.org");
// Events
websocket.onopen = function(evt) {
var params = {
'cmd': 'open'
};
WeBuilderData.SendNative(channel, JSON.stringify(params), '');
};
websocket.onclose = function(evt) {
var params = {
'cmd': 'close'
};
WeBuilderData.SendNative(channel, JSON.stringify(params), '');
};
websocket.onmessage = function(evt) {
var params = {
'cmd': 'message',
'message': evt.data
};
WeBuilderData.SendNative(channel, JSON.stringify(params), '');
};
websocket.onerror = function(evt) {
var params = {
'cmd': 'error',
'message': evt.data
};
WeBuilderData.SendNative(channel, JSON.stringify(params), '');
};
WeBuilder_OnData = function(param_channel, param_message) {
channel = param_channel;
if (param_message != "") {
websocket.send(param_message); //send method
}
}
Blumentals Software Programmer
Re: Support for Websockets in plugins
Great! That would really open up a lot of possibilities.



There are 10 types of people in the world: Those who understand binary and those who don't.