Scorocode.WebSocket
Class for WebSocket connection handling.
An example of implementing a chat application using Scorocode.WebSocket – Scorochat. The application's source code is published on GitHub in the following repository: NikSmith/niksmith.github.io
new WebSocket(channame)
WebSocket channel opening.
Parameter | Type | Properties | Description | Value example |
---|---|---|---|---|
channame | String |
Mandatory | Channel name | "chatroom" |
Example
var WS = new Scorocode.WebSocket('chatroom');
.on(event, callback)
Method for assigning a callback to one of the events:
- open - Connection established
- close - Connection closed
- error - Error
- message - Data received
Parameter | Type | Properties | Description | Value example |
---|---|---|---|---|
event | String |
Mandatory, value from the list | Event to which a callback is assigned | "open", "message", "error", "close" |
callback | Object |
Callback for an event |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey",
WebSocketKey: "webSocketKey"
});
var WS = new Scorocode.WebSocket('Helloworld');
WS.on("open", onOpen () {});
WS.on("close", onClose () {});
WS.on("error", onError () {});
WS.on("message", onMessage(data) {
console.log(data)
});
var data = "Wello Horld";
WS.send(data);
.send(message)
Method for sending a message to channel
Parameter | Type | Properties | Description | Value example |
---|---|---|---|---|
message | String |
Mandatory | Message to be sent to the channel | "Wello Horld" |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey",
WebSocketKey: "webSocketKey"
});
var WS = new Scorocode.WebSocket('Helloworld');
var data = "Wello Horld";
WS.on('open', function(){
WS.send(data);
});