From 7f6ca6eb171b2701624dcfeca15184aa2b1530b8 Mon Sep 17 00:00:00 2001 From: Arnout Kazemier Date: Tue, 8 Mar 2011 22:17:20 +0100 Subject: [PATCH] This should fix https://github.com/LearnBoost/Socket.IO/issues/92 because the client will be broken anyways without JSON support we might as well throw an error. --- lib/transport.js | 13 +++++++++---- socket.io.js | 13 +++++++++---- socket.io.min.js | 2 +- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/transport.js b/lib/transport.js index 9ae8721..913bc6c 100644 --- a/lib/transport.js +++ b/lib/transport.js @@ -16,8 +16,13 @@ stringify = function(message){ if (Object.prototype.toString.call(message) == '[object Object]'){ if (!('JSON' in window)){ - if ('console' in window && console.error) console.error('Trying to encode as JSON, but JSON.stringify is missing.'); - return '{ "$error": "Invalid message" }'; + var error = 'Socket.IO Error: Trying to encode as JSON, but JSON.stringify is missing.'; + if ('console' in window && console.error){ + console.error(error); + } else { + throw new Error(error); + } + return '{ "$error": "'+ error +'" }'; } return '~j~' + JSON.stringify(message); } else { @@ -46,8 +51,8 @@ }; Transport.prototype.encode = function(messages){ - var ret = '', message, - messages = io.util.isArray(messages) ? messages : [messages]; + var ret = '', message; + messages = io.util.isArray(messages) ? messages : [messages]; for (var i = 0, l = messages.length; i < l; i++){ message = messages[i] === null || messages[i] === undefined ? '' : stringify(messages[i]); ret += frame + message.length + frame + message; diff --git a/socket.io.js b/socket.io.js index 023b4db..91ebece 100644 --- a/socket.io.js +++ b/socket.io.js @@ -102,8 +102,13 @@ if (typeof window != 'undefined'){ stringify = function(message){ if (Object.prototype.toString.call(message) == '[object Object]'){ if (!('JSON' in window)){ - if ('console' in window && console.error) console.error('Trying to encode as JSON, but JSON.stringify is missing.'); - return '{ "$error": "Invalid message" }'; + var error = 'Socket.IO Error: Trying to encode as JSON, but JSON.stringify is missing.'; + if ('console' in window && console.error){ + console.error(error); + } else { + throw new Error(error); + } + return '{ "$error": "'+ error +'" }'; } return '~j~' + JSON.stringify(message); } else { @@ -132,8 +137,8 @@ if (typeof window != 'undefined'){ }; Transport.prototype.encode = function(messages){ - var ret = '', message, - messages = io.util.isArray(messages) ? messages : [messages]; + var ret = '', message; + messages = io.util.isArray(messages) ? messages : [messages]; for (var i = 0, l = messages.length; i < l; i++){ message = messages[i] === null || messages[i] === undefined ? '' : stringify(messages[i]); ret += frame + message.length + frame + message; diff --git a/socket.io.min.js b/socket.io.min.js index 6467de6..ff22df9 100644 --- a/socket.io.min.js +++ b/socket.io.min.js @@ -1,2 +1,2 @@ /* Socket.IO.min 0.6.2 @author Guillermo Rauch , @license The MIT license., @copyright Copyright (c) 2010 LearnBoost */ -var io=this.io={version:"0.6.2",setPath:function(a){window.console&&console.error&&console.error("io.setPath will be removed. Please set the variable WEB_SOCKET_SWF_LOCATION pointing to WebSocketMain.swf"),this.path=/\/$/.test(a)?a:a+"/",WEB_SOCKET_SWF_LOCATION=a+"lib/vendor/web-socket-js/WebSocketMain.swf"}};"jQuery"in this&&(jQuery.io=this.io),typeof window!="undefined"&&typeof WEB_SOCKET_SWF_LOCATION=="undefined"&&(WEB_SOCKET_SWF_LOCATION="/socket.io/lib/vendor/web-socket-js/WebSocketMain.swf"),function(){var a=this.io,b=!1;a.util={ios:!1,load:function(a){if(/loaded|complete/.test(document.readyState)||b)return a();"attachEvent"in window?window.attachEvent("onload",a):window.addEventListener("load",a,!1)},inherit:function(a,b){for(var c in b.prototype)a.prototype[c]=b.prototype[c]},indexOf:function(a,b,c){for(var d=a.length,e=c<0?Math.max(0,d+c):c||0;e"),this.doc.parentWindow.s=this,this.doc.close();var a=this.doc.createElement("div");this.doc.body.appendChild(a),this.iframe=this.doc.createElement("iframe"),a.appendChild(this.iframe),this.iframe.src=this.prepareUrl()+"/"+ +(new Date)},b.prototype._=function(a,b){this.onData(a);var c=b.getElementsByTagName("script")[0];c.parentNode.removeChild(c)},b.prototype.destroy=function(){if(this.iframe){try{this.iframe.src="about:blank"}catch(a){}this.doc=null,CollectGarbage()}},b.prototype.disconnect=function(){this.destroy();return a.Transport.XHR.prototype.disconnect.call(this)},b.check=function(){if("ActiveXObject"in window)try{var b=new ActiveXObject("htmlfile");return b&&a.Transport.XHR.check()}catch(c){}return!1},b.xdomainCheck=function(){return!1}}(),function(){var a=this.io,b=a.Transport["xhr-multipart"]=function(){a.Transport.XHR.apply(this,arguments)};a.util.inherit(b,a.Transport.XHR),b.prototype.type="xhr-multipart",b.prototype.get=function(){var a=this;this.xhr=this.request("","GET",!0),this.xhr.onreadystatechange=function(){a.xhr.readyState==4&&a.onData(a.xhr.responseText)},this.xhr.send(null)},b.check=function(){return"XMLHttpRequest"in window&&"prototype"in XMLHttpRequest&&"multipart"in XMLHttpRequest.prototype},b.xdomainCheck=function(){return!0}}(),function(){var a=this.io,b=new Function,c=a.Transport["xhr-polling"]=function(){a.Transport.XHR.apply(this,arguments)};a.util.inherit(c,a.Transport.XHR),c.prototype.type="xhr-polling",c.prototype.connect=function(){if(a.util.ios||a.util.android){var b=this;a.util.load(function(){setTimeout(function(){a.Transport.XHR.prototype.connect.call(b)},10)})}else a.Transport.XHR.prototype.connect.call(this)},c.prototype.get=function(){var a=this;this.xhr=this.request(+(new Date),"GET"),this.xhr.onreadystatechange=function(){var c;if(a.xhr.readyState==4){a.xhr.onreadystatechange=b;try{c=a.xhr.status}catch(d){}c==200?(a.onData(a.xhr.responseText),a.get()):a.onDisconnect()}},this.xhr.send(null)},c.check=function(){return a.Transport.XHR.check()},c.xdomainCheck=function(){return a.Transport.XHR.xdomainCheck()}}(),function(){var a=this.io;a.JSONP=[],JSONPPolling=a.Transport["jsonp-polling"]=function(){a.Transport.XHR.apply(this,arguments),this.insertAt=document.getElementsByTagName("script")[0],this.index=a.JSONP.length,a.JSONP.push(this)},a.util.inherit(JSONPPolling,a.Transport["xhr-polling"]),JSONPPolling.prototype.type="jsonp-polling",JSONPPolling.prototype.sendIORequest=function(a){function h(){b.iframe&&b.form.removeChild(b.iframe);try{f=document.createElement('