50 lines
1.8 KiB
JavaScript
50 lines
1.8 KiB
JavaScript
$(function () {
|
||
|
||
//判断浏览器是否支持WebSocket
|
||
var supportsWebSockets = 'WebSocket' in window || 'MozWebSocket' in window;
|
||
if (supportsWebSockets) {
|
||
//建立WebSocket连接(ip地址换成自己主机ip)
|
||
var ws = new WebSocket("wss://fat-hk-ws-sdk.szfiu.com/websocket");
|
||
ws.onopen = function () {
|
||
//当WebSocket创建成功时,触发onopen事件
|
||
console.log("websocket连接成功");
|
||
//ws.send("hello"); //将消息发送到服务端
|
||
ws.send(
|
||
JSON.stringify({
|
||
symbols: "['00700.hk]",
|
||
command: 0,
|
||
version: '1.0.0',
|
||
token: 'szfiu',
|
||
heartBeat: 'heartBeat'
|
||
})
|
||
);
|
||
}
|
||
ws.onmessage = function (e) {
|
||
//当客户端收到服务端发来的消息时,触发onmessage事件,参数e.data包含server传递过来的数据
|
||
console.log("收到数据");
|
||
console.log(e.data);
|
||
var eqData = e.data.content
|
||
LineInputLeftRandomBar(30 - 72)
|
||
LineInputRightRandomBar(66 - 72)
|
||
Microphone1LeftRandomBar(34 - 72)
|
||
Microphone2LeftRandomBar(67 - 72)
|
||
OutputVolumeLeftRandomBar(39 - 72);
|
||
OutputVolumeRightRandomBar(87 - 72);
|
||
}
|
||
ws.onclose = function (e) {
|
||
//当客户端收到服务端发送的关闭连接请求时,触发onclose事件
|
||
console.log("websocket已断开");
|
||
}
|
||
ws.onerror = function (e) {
|
||
//如果出现连接、处理、接收、发送数据失败的时候触发onerror事件
|
||
console.log("websocket发生错误" + e);
|
||
}
|
||
|
||
|
||
} else {
|
||
|
||
layer.alert("您的浏览器不支持 WebSocket!");
|
||
|
||
}
|
||
|
||
}); |