qd-changjing/src/supermap/roomservice.js

153 lines
4.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Description: atmosphere服务类
* Author: tiansiyuan
* Date: 2020/5/16 13:49:15
*/
import store from '@/store';
import { updateStatusbyRoomIdAndUserId } from '@/api/plotting/plotting';
const atmosphere = window.atmosphere || {};
const RoomService = {
/**
* 创建标绘对象
* url标绘room的地址
*/
create(url) {
const service = {
showPrompt: true,
parent: null,
message: {
onOpen: "连接标绘房间成功",
onClientTimeout: "消息发送失败",
onError: "标绘服务器内部错误,请刷新页面重新接入",
onReconnect: "掉线重连....请稍候",
onTransportFailure: "客户端不支持websocket请更换浏览器",
},
onMessage(msg) {},
onReopen() {}
};
let connectedEndPoint = null;
service.join = function(userType, id, roomId) {
debugger;
// 如果url为空说明还没有创建协同标绘房间。
// 如果connectedEndPoint不为空说明当前已经连接到一个协同标绘房间重新连接需要先退出当前房间。
if (url !== '' && connectedEndPoint == null) {
let request = {
url: url,
transport: 'websocket',
fallbackTransport: 'websocket', // 只支持websocket方式
logLevel: 'debug',
maxReconnectOnClose: 3, // 重连三次
reconnectInterval: 15000, // 每次重连的时间间隔为2秒
reconnectOnServerError: true,
// 成功建立连接回调函数
onOpen(response) {
console.log('成功链接', response);
if (service.showPrompt) {
console.log('标绘房间连接成功!');
}
switch (userType) {
case 'platform':
if (id !== store.getters.hostOrgId) {
service.send({
id: id,
status: 1,
userType: userType,
roomId: roomId,
type: 'status',
});
}
break;
case 'app':
service.send({
id: id,
status: 1,
userType: userType,
roomId: roomId,
type: 'status',
});
updateStatusbyRoomIdAndUserId(roomId, id, 1);
break;
default:
}
},
onReopen(request, response) {
location.reload();
},
// 收到atmosphere消息的回调函数
onMessage(response) {
if (response.state === 'messageReceived' && response.status === 200) {
if (service.onMessage != null && typeof (service.onMessage) == 'function') {
service.onMessage(response.responseBody);
}
}
},
// 客户端request超时回调函数
onClientTimeout(request) {
if (service.showPrompt) {
console.log('onClientTimeout');
}
},
// 收到未知错误回调函数
onError(response) {
console.log('res', response);
if (service.showPrompt) {
console.log('onError');
}
},
// 成功重建连接回调函数
onReconnect(request, response) {
if (service.showPrompt) {
}
},
// transport类型不支持回调函数
onTransportFailure(errMsg, request) {
if (service.showPrompt) {
}
}
};
connectedEndPoint = atmosphere.subscribe(request);
} else {
console.log('No room has been created or connection has been established already!');
}
};
/**
* 退出标绘room
*/
service.leave = function() {
atmosphere.unsubscribe();
connectedEndPoint = null;
};
/**
* 发送标绘内容
*/
service.send = function(message) {
console.log('通过atmosphere发送的消息%o', message);
if (connectedEndPoint == null) {
console.log('please establish the connection before sending message!');
return false;
}
if (connectedEndPoint.request.isOpen === false) {
console.log('current connection is closed!');
return false;
}
const msg = JSON.stringify(message);
connectedEndPoint.push(msg);
};
return service;
}
};
export default RoomService;