Support for Websockets in plugins

Let us know what you would like to see in the next version of this software

Moderator: kfury77

Forum rules
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".

Please note that we DO READ all suggestions, even if a reply is not posted. Thanks!

Support for Websockets in plugins

Postby pmk65 » Tue May 30, 2017 1:32 pm

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.
There are 10 types of people in the world: Those who understand binary and those who don't.
User avatar
pmk65
 
Posts: 564
Joined: Sun Dec 20, 2009 9:58 pm
Location: Copenhagen, Denmark

Re: Support for Websockets in plugins

Postby pmk65 » Thu Jun 15, 2017 8:03 pm

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.
There are 10 types of people in the world: Those who understand binary and those who don't.
User avatar
pmk65
 
Posts: 564
Joined: Sun Dec 20, 2009 9:58 pm
Location: Copenhagen, Denmark

Re: Support for Websockets in plugins

Postby Aivars » Fri Jun 16, 2017 12:10 pm

Is this like a built-in on-demand server? Are there any security implications?
Blumentals Software Programmer
User avatar
Aivars
Blumentals Software Developer
 
Posts: 2265
Joined: Thu Aug 22, 2002 1:40 pm
Location: Latvia

Re: Support for Websockets in plugins

Postby pmk65 » Fri Jun 16, 2017 2:27 pm

Websockets is just a communications protocol. It supports CORS.

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


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/websockets/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.
User avatar
pmk65
 
Posts: 564
Joined: Sun Dec 20, 2009 9:58 pm
Location: Copenhagen, Denmark

Re: Support for Websockets in plugins

Postby pmk65 » Thu Mar 08, 2018 2:05 pm

*Bump*
There are 10 types of people in the world: Those who understand binary and those who don't.
User avatar
pmk65
 
Posts: 564
Joined: Sun Dec 20, 2009 9:58 pm
Location: Copenhagen, Denmark

Re: Support for Websockets in plugins

Postby Aivars » Tue Mar 13, 2018 7:49 pm

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:
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
User avatar
Aivars
Blumentals Software Developer
 
Posts: 2265
Joined: Thu Aug 22, 2002 1:40 pm
Location: Latvia

Re: Support for Websockets in plugins

Postby pmk65 » Thu Mar 15, 2018 2:45 pm

Great! That would really open up a lot of possibilities. 8) :o
There are 10 types of people in the world: Those who understand binary and those who don't.
User avatar
pmk65
 
Posts: 564
Joined: Sun Dec 20, 2009 9:58 pm
Location: Copenhagen, Denmark


Return to HTMLPad / Rapid CSS / Rapid PHP / WeBuilder Feature Requests

Who is online

Users browsing this forum: No registered users and 3 guests

cron