|
|
// import axios from "axios";
|
|
|
// let base = 'empt-location/userPayOrder';
|
|
|
// var socket;
|
|
|
//
|
|
|
// function openSocket(orderNum) {
|
|
|
// console.log(orderNum);
|
|
|
// if(typeof(WebSocket) == "undefined") {
|
|
|
// console.log("您的浏览器不支持WebSocket");
|
|
|
// }else{
|
|
|
// console.log("您的浏览器支持WebSocket");
|
|
|
// //实现化WebSocket对象,指定要连接的服务器地址与端口 建立连接
|
|
|
// //等同于socket = new WebSocket("ws://localhost:8888/xxxx/im/25");
|
|
|
// //var socketUrl="${request.contextPath}/im/"+$("#userId").val();
|
|
|
// var socketUrl="http://localhost:10003/imserver/"+orderNum;
|
|
|
// socketUrl=socketUrl.replace("https","ws").replace("http","ws");
|
|
|
// console.log(socketUrl);
|
|
|
// if(socket!=null){
|
|
|
// socket.close();
|
|
|
// socket=null;
|
|
|
// }
|
|
|
// socket = new WebSocket(socketUrl);
|
|
|
// //打开事件
|
|
|
// socket.onopen = function() {
|
|
|
// console.log("websocket已打开");
|
|
|
// //socket.send("这是来自客户端的消息" + location.href + new Date());
|
|
|
// };
|
|
|
// //获得消息事件
|
|
|
// socket.onmessage = function(msg) {
|
|
|
//
|
|
|
// console.log(msg.data);
|
|
|
// return msg;
|
|
|
// //发现消息进入 开始处理前端触发逻辑
|
|
|
// };
|
|
|
// //关闭事件
|
|
|
// socket.onclose = function() {
|
|
|
// console.log("websocket已关闭");
|
|
|
// };
|
|
|
// //发生了错误事件
|
|
|
// socket.onerror = function() {
|
|
|
// console.log("websocket发生了错误");
|
|
|
// }
|
|
|
// }
|
|
|
// }
|
|
|
// function sendMessage() {
|
|
|
// if(typeof(WebSocket) == "undefined") {
|
|
|
// console.log("您的浏览器不支持WebSocket");
|
|
|
// }else {
|
|
|
// console.log("您的浏览器支持WebSocket");
|
|
|
// console.log('{"toUserId":"'+$("#toUserId").val()+'","contentText":"'+$("#contentText").val()+'"}');
|
|
|
// socket.send('{"toUserId":"'+$("#toUserId").val()+'","contentText":"'+$("#contentText").val()+'"}');
|
|
|
// }
|
|
|
// }
|
|
|
//
|
|
|
//
|
|
|
// export default {
|
|
|
// openSocket
|
|
|
// }
|
|
|
|
|
|
const WSS_URL = `ws://localhost:10003/imserver`
|
|
|
let Socket = ''
|
|
|
let setIntervalWesocketPush = null
|
|
|
|
|
|
/**建立连接 */
|
|
|
export function createSocket() {
|
|
|
var socketUrl="http://localhost:10003/imserver";
|
|
|
socketUrl=socketUrl.replace("https","ws").replace("http","ws");
|
|
|
|
|
|
console.log(socketUrl);
|
|
|
if (!Socket) {
|
|
|
console.log('建立websocket连接')
|
|
|
Socket = new WebSocket(socketUrl)
|
|
|
Socket.onopen = onopenWS
|
|
|
Socket.onmessage = onmessageWS
|
|
|
Socket.onerror = onerrorWS
|
|
|
Socket.onclose = oncloseWS
|
|
|
} else {
|
|
|
console.log('websocket已连接')
|
|
|
}
|
|
|
}
|
|
|
/**打开WS之后发送心跳 */
|
|
|
export function onopenWS() {
|
|
|
sendPing() //发送心跳
|
|
|
}
|
|
|
/**连接失败重连 */
|
|
|
export function onerrorWS() {
|
|
|
clearInterval(setIntervalWesocketPush)
|
|
|
Socket.close()
|
|
|
createSocket() //重连
|
|
|
}
|
|
|
/**WS数据接收统一处理 */
|
|
|
export function onmessageWS(e) {
|
|
|
console.log(e.data);
|
|
|
window.dispatchEvent(new CustomEvent('onmessageWS', {
|
|
|
detail: {
|
|
|
data: e
|
|
|
}
|
|
|
}))
|
|
|
}
|
|
|
/**发送数据
|
|
|
* @param eventType
|
|
|
*/
|
|
|
export function sendWSPush(eventTypeArr) {
|
|
|
const obj = {
|
|
|
appId: 'airShip',
|
|
|
cover: 0,
|
|
|
event: eventTypeArr
|
|
|
}
|
|
|
if (Socket !== null && Socket.readyState === 3) {
|
|
|
Socket.close()
|
|
|
createSocket() //重连
|
|
|
} else if (Socket.readyState === 1) {
|
|
|
Socket.send(JSON.stringify(obj))
|
|
|
} else if (Socket.readyState === 0) {
|
|
|
setTimeout(() => {
|
|
|
Socket.send(JSON.stringify(obj))
|
|
|
}, 3000)
|
|
|
}
|
|
|
}
|
|
|
/**关闭WS */
|
|
|
export function oncloseWS() {
|
|
|
clearInterval(setIntervalWesocketPush)
|
|
|
console.log('websocket已断开')
|
|
|
}
|
|
|
/**发送心跳 */
|
|
|
export function sendPing() {
|
|
|
Socket.send('ping')
|
|
|
setIntervalWesocketPush = setInterval(() => {
|
|
|
Socket.send('ping')
|
|
|
}, 5000)
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|