1

之前的项目,由于要照顾低端机型不支持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);//发送
}

参考js:http://async-io.org/tutorial.html


dada86
993 声望22 粉丝

多努力一点点。