nyx_server
Version:
Node内容发布
161 lines (125 loc) • 4.29 kB
JavaScript
(function (win , doc , $) {
"use strict";
/**
* @param msgManager
* @param options
* {
* serverUrl: 服务器地址
* dataType: string | json
* }
*/
var WS = function(msgManager , options){
var _serverUrl = options.serverUrl;
var dataType = 'undefined' != typeof options.dataType ? options.dataType : 'json';
var websocket;
var connectState;
this.open = function () {
var Socket = win.MozWebSocket || win.WebSocket;
websocket = new Socket(_serverUrl);
websocket.onopen = function () {
connectState = "completed";
msgManager.onOpen();
};
websocket.onmessage = function (ev) {
var result = ev.data;
if(dataType == 'json'){
result = JSON.parse(ev.data);
}
msgManager.onData(result);
};
websocket.onclose = function (e) {
connectState = "";
msgManager.onClose();
};
websocket.onerror = function (e) {
msgManager.onError(e);
};
};
this.send = function(msg){
//将转换的对象转为文本类型,暂时很浏览器和服务器不支持二进制
if(connectState =='completed'){ //如果连接成功才能发送数据
websocket.send(JSON.stringify(msg));
}
};
this.type = 'WebSocket';
};
//=========================================================
/**
* @param msgManager
* @param options
* {
* {string} serverUrl: 服务器地址
* {string} dataType: string | json
* {string} type : post | get
* }
*/
var XHR = function(msgManager , options){
var _serverUrl = options.serverUrl;
var datatype = 'undefined' != typeof options.dataType ? options.dataType : 'json';
var type = 'undefined' != typeof options.type ? options.type : 'post';
this.open = function(){
msgManager.onOpen();
};
this.send = function(data){
$.ajax({
'type': type,
'dataType' : datatype,
'url': _serverUrl,
'data': jquery.param(data), //这里先使用Jquery的方法转换参数
'success' : function(data){
msgManager.onData(data);
},
'error' : function(e){
msgManager.onError(e);
}
});
};
this.type = 'XHR';
};
// JSONP 连接
/**
* @param msgManager
* @param options
* {
* {string} serverUrl: 服务器地址
* {string} callback 传给服务器的回调方法名
* }
*/
var JSONP = function(msgManager , options){
var _serverUrl = options.serverUrl;
//var datatype = 'undefined' != typeof options.dataType ? options.dataType : 'json';
//var type = 'undefined' != typeof options.type ? options.type : 'get';
var callback = 'undefined' != typeof options.callback ? options.callback : '__jsonpcb__';
this.open = function(){
msgManager.onOpen();
};
var index = 0;
this.send = function(data){
get(data);
};
function get(data){
index++;
var _url = _serverUrl;
if (_url.indexOf("?") < 0) {
_url = _url + "?";
} else {
_url = _url + "&";
}
var script = doc.createElement('script');
script.async = true;
script.src = _url + jquery.param(data)+"&callback="+callback+'&t='+ (+new Date()) + '&i=' + index;
script.onerror = function () {
msgManager.onClose();
};
var insertAt = doc.getElementsByTagName('script')[0];
insertAt.parentNode.insertBefore(script, insertAt);
win[callback] = function(){
msgManager.onData([].slice.apply(arguments));
};
}
this.type = 'JSONP';
};
win.WS = WS;
win.XHR = XHR;
win.JSONP= JSONP;
})(window , document , typeof topicJquery !== 'undefined' ? topicJquery : jQuery);