Skip to content

Commit

Permalink
This should fix socketio/socket.io#92 because the client will be brok…
Browse files Browse the repository at this point in the history
…en anyways without JSON support we might as well throw an error.
  • Loading branch information
sunrise30 committed Mar 8, 2011
1 parent 97e5bf8 commit 269691c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
13 changes: 9 additions & 4 deletions lib/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 9 additions & 4 deletions socket.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 269691c

Please # to comment.