之前的项目,由于要照顾低端机型不支持websocket进行通信,选择了atmosphere.js,
在不支持websocket的环境下,使用long-polling长轮询方式进行,很好用。特做个笔记。
$(function () {
var request = {
url: ajaxServerUrl + '/chat/doctor/' + data.relationId,
contentType: "application/json",
logLevel: 'debug',
transport: 'websocket',
trackMessageLength: true,
reconnectInterval: 5000,
fallbackTransport: 'long-polling'
};
request.onOpen = function(response) {
uuid = response.request.uuid;
$.atmosphere.subscribe(request).push(atmosphere.util.stringifyJSON({
clientId: localStorage.id,
type: '2',
uuid: uuid
}));
console.log("聊天开始了:"+localStorage.id);
};
request.onMessage = function (response) {//监听
console.log(response.responseBody);
var msgObj = atmosphere.util.parseJSON(response.responseBody);
};
request.onClose = function(response) {
// subSocket.push(atmosphere.util.stringifyJSON({author: author, message: 'disconnecting'}));
};
request.onError = function(response) {
logged = false;
};
request.onReconnect = function(request, response) {
// content.html($('<p>', {text: 'Connection lost, trying to reconnect. Trying to reconnect ' + request.reconnectInterval}));
// input.attr('disabled', 'disabled');
};
$.atmosphere.subscribe(request);//发送
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。