Add the following configuration to the features attribute in the manifest.json file:
{"name": "system.websocketfactory"} Add the following configuration to the script element on the page where the API will be called:
import websocketfactory from '@system.websocketfactory'
Or
var websocketfactory = require("@system.websocketfactory") Item | Description |
|---|---|
Applicable devices | Mobile phones, tablets, Vision products, and telematics devices |
Applicable regions | Global |
API | Description |
|---|---|
Creates a WebSocket connection. Each time this API is successfully called, a new websocket object is returned. | |
Uses the websocket object created to send a message. | |
Closes a WebSocket connection. | |
Listens to a WebSocket connection. | |
Listens to message events and receives messages from the server. | |
Listens to connection close events. | |
Listens to error events. |
Description
Creates a WebSocket connection. Each time this API is successfully called, a new websocket object is returned.
In 1060 and later versions, you can use config.network in the manifest.json file to configure the network timeout.
The number of websocket objects that can be created is not limited.
OBJECT parameters
Parameter | Type | Mandatory/Optional | Description |
|---|---|---|---|
url | string | Mandatory | API address of your server, which must start with wss or ws. |
header | object | Optional | HTTP header, which cannot contain Referer. |
protocols | array | Optional | Protocols. |
Sample code
var websocket = websocketfactory.create({
url: 'ws://echo.websocket.org',
header:{
'content-type': 'application/json'
},
protocols: ['protocol1']
}) Description
Uses the websocket object created to send a message.
OBJECT parameters
Parameter | Type | Default Value | Mandatory/Optional | Description |
|---|---|---|---|---|
data | string | arraybuffer | - | Mandatory | Data to be sent to the server. The data size cannot exceed 16 MB. |
success | function | - | Optional | Callback upon success. |
fail | function | - | Optional | Callback upon failure. |
Sample code
websocket.send({
data:'send message',
success:function() {
console.log('success');
},
fail:function() {
console.log('failed');
}
})
sendMessage: function () {
var that = this;
var buffer = new ArrayBuffer(4);
var a = new Uint8Array(buffer);
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
if (that.websocket) {
that.websocket.send({
data: buffer,
success: function () {
console.log('success');
that.isSend = "Sending successful.";
},
fail: function () {
console.log('failed');
that.isSend = "Sending failed.";
}
})
}
}, Description
Closes a WebSocket connection.
OBJECT parameters
Parameter | Type | Default Value | Mandatory/Optional | Description |
|---|---|---|---|---|
code | number | 1000 | Optional | Status number of a closed connection. |
reason | string | - | Optional | Reason for closing a connection. |
success | function | - | Optional | Callback upon success. |
fail | function | - | Optional | Callback upon failure. |
Sample code
websocket.close({
code: 1000,
reason: 'close as normal',
success: function() {
console.log('close success');
},
fail: function() {
console.log('close fail');
}
}) Description
Listens to a WebSocket connection.
OBJECT parameters
Parameter | Type | Mandatory/Optional | Description |
|---|---|---|---|
callback | function | Optional | Connection callback. |
Sample code
websocket.onopen = function() {
console.log('connect open');
} Description
Listens to message events and receives messages from the server.
OBJECT parameters
Parameter | Type | Mandatory/Optional | Description |
|---|---|---|---|
callback | function | Optional | Callback of returning a message to the server. |
Parameters returned in callback
Parameter | Type | Description |
|---|---|---|
data | string | arraybuffer | Message received by the listener. |
Sample code
websocket.onmessage = function (e) {
console.log("onmessage e = "+e.data);
}
websocket.onmessage = function (e) {
console.log("onmessage e = "+JSON.stringify(String.fromCharCode.apply(null, new Uint8Array(e.data))));
} Description
Listens to connection close events.
OBJECT parameters
Parameter | Type | Mandatory/Optional | Description |
|---|---|---|---|
callback | function | Optional | Callback of connection close events. |
Parameters returned in callback
Parameter | Type | Description |
|---|---|---|
code | number | Close status code returned by the server. |
reason | string | Close reason returned by the server. |
wasClean | boolean | Indicates a connection is completely closed. |
Code description
Result Code | Method |
|---|---|
1000 | The connection is closed normally. |
1001 | Going Away |
1002 | Protocol error. |
1003 | Unsupported data. |
1005 | No status received. |
1006 | The connection is closed abnormally. |
1007 | Invalid frame payload data |
1008 | Policy Violation |
1009 | The message is too large. |
1010 | Missing extension. |
1011 | Internal Error |
1012 | Service Restart |
1013 | Try again later. |
1014 | Bad gateway. |
1015 | TLS handshake. |
Sample code
websocket.onclose = function(e) {
console.log("onclose e.code = "+e.code+", e.reason = "+e.reason+", wasClean = "+e.wasClean);
} Description
Listens to error events.
OBJECT parameters
Parameter | Type | Mandatory/Optional | Description |
|---|---|---|---|
callback | function | Optional | Error message event. |
Parameters returned in callback
Parameter | Type | Description |
|---|---|---|
data | string | Message received by the listener. |
Sample code
websocket.onerror = function(e) {
console.log("onerror e.data = "+e.data);
} <template>
<div>
<div>
<text>{{componentName}}</text>
</div>
<div>
<text>Connect to the specified URL: {{isSuccess}}</text>
</div>
<input type="button" onclick="connectWebsocket" value="Connect to the specified URL." />
<div>
<text>Connect to the specified URL 2: {{isSuccess2}}</text>
</div>
<input type="button" onclick="connectWebsocket2" value="Connect to the specified URL 2." />
<div>
<text>Connect to the specified URL 3: {{isSuccess3}}</text>
</div>
<input type="button" onclick="connectWebsocket3" value="Connect to the specified URL 3." />
<div>
<text>Send a message: {{isSend}}</text>
</div>
<input type="button" onclick="sendMessage" value="Send a message." />
<input type="button" onclick="closeWebsocket" value="Close" />
<div>
<text>Send a message: {{isSend2}}</text>
</div>
<input type="button" onclick="sendMessage2" value="Send message 2." />
<input type="button" onclick="closeWebsocket2" value="Close 2" />
<div>
<text>Send a message: {{isSend3}}</text>
</div>
<input type="button" onclick="sendMessage3" value="Send message 3." />
<input type="button" onclick="closeWebsocket3" value="Close 3" />
</div>
</template>
<style>
@import '../Common/css/common.css';
.item-container {
margin-bottom: 50px;
margin-right: 60px;
margin-left: 60px;
flex-direction: column;
}
.item-content {
flex-direction: column;
background-color: #ffffff;
padding-left: 30px;
padding-right: 30px;
padding-top: 20px;
padding-bottom: 20px;
margin-bottom: 30px;
align-items: flex-start;
}
.txt {
padding-top: 15px;
padding-bottom: 15px;
}
</style>
<script>
import websocketfactory from '@system.websocketfactory'
export default {
data: {
componentName: 'Websocket',
isSuccess: '',
isSend: '',
isSend2: '',
isSend3: '',
websocket: '',
isSuccess2: '',
isSuccess3: '',
websocket2: '',
},
onInit: function () {
this.$page.setTitleBar({ text: 'Websocket' })
},
connectWebsocket: function () {
var that = this;
that.websocket = websocketfactory.create({
url: 'ws://echo.websocket.org'
})
that.websocket.onmessage = function (e) {
console.log("onmessage e = " + e.data);
}
that.websocket.onclose = function (e) {
console.log("onclose e.code = " + e.code + ", e.reason = " + e.reason + ", wasClean = " + e.wasClean);
}
that.websocket.onerror = function (e) {
console.log("onerror e.data = " + e.data);
that.isSuccess = "Connection failed.";
}
that.websocket.onopen = function () {
console.log('connect open');
that.isSuccess = "Connection successful.";
}
},
sendMessage: function () {
var that = this;
if (that.websocket) {
that.websocket.send({
data: 'send message',
success: function () {
console.log(' success');
that.isSend = "Sending successful.";
},
fail: function () {
console.log('failed');
that.isSend = "Sending failed.";
}
})
}
},
closeWebsocket: function () {
var that = this;
if (that.websocket) {
that.websocket.close({
code: 1000,
reason: 'close as normal',
success: function () {
console.log('close success!!!');
},
fail: function () {
console.log('close fail');
}
})
}
},
connectWebsocket2: function () {
var that = this;
that.websocket2 = websocketfactory.create({
url: 'ws://echo.websocket.org',
header: {
'content-type': 'application/json'
},
protocols: ['protocol1']
})
that.websocket2.onmessage = function (e) {
console.log("2 onmessage e = " + e.data);
}
that.websocket2.onclose = function (e) {
console.log("2 onclose e.code = " + e.code + ", e.reason = " + e.reason);
}
that.websocket2.onerror = function (e) {
console.log("2 onerror e.data = " + e.data);
that.isSuccess2 = "Connection failed.";
}
that.websocket2.onopen = function () {
console.log('2 connect open');
that.isSuccess2 = "Connection successful.";
}
},
sendMessage2: function () {
var that = this;
if (that.websocket2) {
that.websocket2.send({
data: 'send2 message',
success: function () {
console.log('success2');
that.isSend2 = "Sending successful.";
},
fail: function () {
console.log('failed 2');
that.isSend2 = "Sending failed.";
}
})
}
},
closeWebsocket2: function () {
var that = this;
if (that.websocket2) {
that.websocket2.close({
code: 1000,
reason: 'close as normal',
success: function () {
console.log('close2 success!!!');
},
fail: function () {
console.log('close2 fail');
}
})
}
},
connectWebsocket3: function () {
var that = this;
that.websocket3 = websocketfactory.create({
url: 'ws://echo.websocket.org',
header: {
'content-type': 'application/json'
},
protocols: ['protocol1']
})
that.websocket3.onmessage = function (e) {
console.log("3 onmessage e = " + JSON.stringify(String.fromCharCode.apply(null, new Uint8Array(e.data))));
}
that.websocket3.onclose = function (e) {
console.log("3 onclose e.code = " + e.code + ", e.reason = " + e.reason);
}
that.websocket3.onerror = function (e) {
console.log("3 onerror e.data = " + e.data);
that.isSuccess3 = "Connection failed.";
}
that.websocket3.onopen = function () {
console.log('3 connect open');
that.isSuccess3 = "Connection successful.";
}
},
sendMessage3: function () {
var that = this;
var buffer = new ArrayBuffer(4);
var a = new Uint8Array(buffer);
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
if (that.websocket3) {
that.websocket3.send({
data: buffer,
success: function () {
console.log('success3');
that.isSend3 = "Sending successful.";
},
fail: function () {
console.log('failed 3');
that.isSend3 = "Sending failed.";
}
})
}
},
closeWebsocket3: function () {
var that = this;
if (that.websocket3) {
that.websocket3.close({
code: 1000,
reason: 'close as normal',
success: function () {
console.log('close3 success!!!');
},
fail: function () {
console.log('close3 fail');
}
})
}
}
}
</script>