异步通信atmosphere.js
之前的项目,由于要照顾低端机型不支持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($('', {text: 'Connection lost, trying to reconnect. Trying to reconnect ' + request.reconnectInterval})); // input.attr('disabled', 'disabled');}; $.atmosphere.subscribe(request);//发送}
参考js:http://async-io.org/tutorial.html
关键字:JavaScript
版权声明
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处。如若内容有涉嫌抄袭侵权/违法违规/事实不符,请点击 举报 进行投诉反馈!