WebSocket

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.

TypeScript code blocks include examples of how to implement, override and components within the theme. You can create page with the UI Editor to make your page compatible with theming and then you can implement themable components programmatically. Once the page is created with the UI Editor, it generates a class under scripts/generated/pages. You can then extend that class with the following TypeScript classes.

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.

TypeScript
JavaScript
TypeScript
import PageSampleDesign from 'generated/pages/pageSample';
import FlexLayout = require('sf-core/ui/flexlayout');
import Application = require('sf-core/application');
import WebSocket = require('sf-core/net/websocket');
import Blob = require('sf-core/global/blob');
//You should create new Page from UI-Editor and extend with it.
export default class Sample extends PageSampleDesign {
myWebSocket: WebSocket;
constructor() {
super();
// Overrides super.onShow method
this.onShow = onShow.bind(this, this.onShow.bind(this));
// Overrides super.onLoad method
this.onLoad = onLoad.bind(this, this.onLoad.bind(this));
this.layout.flexDirection = FlexLayout.FlexDirection.COLUMN;
this.layout.justifyContent = FlexLayout.JustifyContent.CENTER;
this.layout.alignItems = FlexLayout.AlignItems.CENTER;
}
}
/**
* @event onShow
* This event is called when a page appears on the screen (everytime).
* @param {function} superOnShow super onShow function
* @param {Object} parameters passed from Router.go function
*/
function onShow(superOnShow: () => void) {
const { headerBar } = this;
superOnShow();
Application.statusBar.visible = false;
headerBar.visible = false;
}
/**
* @event onLoad
* This event is called once when page is created.
* @param {function} superOnLoad super onLoad function
*/
function onLoad(superOnLoad: () => void) {
superOnLoad();
this.myWebSocket = new WebSocket({ url: "wss://echo.websocket.org" });
this.myWebSocket.onOpen = (): void => {
console.log("Web socket opened.");
console.log("Send string");
this.myWebSocket.send({ data: "some string" });
};
this.myWebSocket.close = (params: {code: number, reason?: string}) => {
console.log("onClose parameters : " + JSON.stringify(params));
};
this.myWebSocket.onMessage = (e: {string: string, blob: Blob}) => {
console.log("Message received.");
this.myWebSocket.close({ code: 1000 });
};
}
JavaScript
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;