API Reference: Net.WebSocket

WebSocket creates a websocket client. It is very simple to use. After you pass url parameter in constructor(this is mandatory) a websocket instance is created and onOpen callback triggers.

Reopen WebSocket

You can't send data after the websocket is closed. If you need, you must create a new web socket instance. Websocket isn't reopened automatically when send method calls if the websocket is closed.

const Page = require("sf-core/ui/page");
const WebSocket = require('sf-core/net/websocket');
const extend = require("js-base/core/extend");
var Page1 = extend(Page)(
function(_super) {
_super(this, {
onShow: function(params) {
Application.statusBar.visible = false;
this.headerBar.visible = false;
var myWebSocket = new WebSocket({url: "wss://echo.websocket.org"});
myWebSocket.onOpen = function() {
console.log("Web socket opened.");
console.log("Send string");
myWebSocket.send({data: "some string"});
myWebSocket.close = function(e) {
console.log("onClose parameters : " + JSON.stringify(e));
myWebSocket.onMessage = function(e) {
console.log("Message received.");
myWebSocket.close({code: 1000});
module.exports = Page1;