TypeError: Cannot convert 'document.body' to object
I'm using 0.7 and Opera 11.10
(Opera/9.80 (X11; Linux x86_64; U; it) Presto/2.8.131 Version/11.10)
I get this error:
Uncaught exception: TypeError: Cannot convert 'document.body' to object
Error thrown at line 3391, column 6 in <anonymous function: JSONPPolling.prototype.post>(data) in http://damiano.local:9000/socket.io/socket.io.js:
document.body.appendChild(form);
called from line 2880, column 4 in <anonymous function: XHR.prototype.send>(data) in http://damiano.local:9000/socket.io/socket.io.js:
this.post(data);
called from line 2868, column 4 in <anonymous function: XHR.prototype.payload>(payload) in http://damiano.local:9000/socket.io/socket.io.js:
this.send(io.parser.encodePayload(msgs));
called from line 1684, column 6 in <anonymous function: Socket.prototype.setBuffer>(v) in http://damiano.local:9000/socket.io/socket.io.js:
this.transport.payload(this.buffer);
called from line 1750, column 6 in <anonymous function: Socket.prototype.onConnect>() in http://damiano.local:9000/socket.io/socket.io.js:
this.setBuffer(false);
called from line 1294, column 4 in <anonymous function: Transport.prototype.onConnect>() in http://damiano.local:9000/socket.io/socket.io.js:
this.socket.onConnect();
called from line 1250, column 6 in <anonymous function: Transport.prototype.onPacket>(packet) in http://damiano.local:9000/socket.io/socket.io.js:
return this.onConnect();
called from line 1230, column 10 in <anonymous function: Transport.prototype.onData>(data) in http://damiano.local:9000/socket.io/socket.io.js:
this.onPacket(msgs[i]);
called from line 3475, column 4 in <anonymous function: JSONPPolling.prototype._>(msg) in http://damiano.local:9000/socket.io/socket.io.js:
this.onData(msg);
called from line 3345, column 6 in <anonymous function>(msg) in http://damiano.local:9000/socket.io/socket.io.js:
self._(msg);
Got a reproducible test case?
SERVER:
var io = require('socket.io'), fs = require('fs'), http = require('http');
var app = http.createServer(), io = io.listen(app);
app.listen(9000);
io.set('log level', 3);
io.sockets.on('connection', function(socket) {
console.log("New connection");
socket.on('message', function (message) {
console.log("Received: " + message);
socket.emit('custom', {text:"Testo di risposta del server"});
});
});
CLIENT:
damiano.local is 127.0.0.1 (/etc/hosts)
I provide the client using NGINX at port number 85 (damiano.local:85) and NODE is listening at 9000 as you can see.
As I wrote i'm using Opera 11.10, the complete user agent is:
Opera/9.80 (X11; Linux x86_64; U; it) Presto/2.8.131 Version/11.10
Hope this help.
I can still reproduce this using socket.io 0.7.7, now 0.7.0, node 0.4.7, opera 11.50 on Windows. NPM says:
[email protected] =sridatta active installed NowJS: An easy to use real-time RPC library
[email protected] =rauchg installed latest remote Real-time apps made cross-browser & easy with a WebSocket-like API websocket s
[email protected] =rauchg active installed latest remote Socket.IO client for the browser and node.js websocket socket realtime
Can reproduce with the following: Client:
<html><head><script src="http://vali.dobarkod.hr:7070/nowjs/now.js"></script></head><body></body></html>
Server:
var
http = require('http'),
nowjs = require('now'),
server = http.createServer(function (req, res) {})
everyone = nowjs.initialize(server);
server.listen(7070);
Opera says:
Uncaught exception: TypeError: Cannot convert 'document.body' to object
Error thrown at line 3420, column 6 in <anonymous function: JSONPPolling.prototype.post>(data) in http://vali.dobarkod.hr:7070/socket.io/socket.io.js:
document.body.appendChild(form);
called from line 2910, column 4 in <anonymous function: XHR.prototype.send>(data) in http://vali.dobarkod.hr:7070/socket.io/socket.io.js:
this.post(data);
called from line 1336, column 4 in <anonymous function: Transport.prototype.packet>(packet) in http://vali.dobarkod.hr:7070/socket.io/socket.io.js:
this.send(io.parser.encodePacket(packet));
called from line 1667, column 6 in <anonymous function: Socket.prototype.packet>(data) in http://vali.dobarkod.hr:7070/socket.io/socket.io.js:
this.transport.packet(data);
called from line 1965, column 4 in <anonymous function: SocketNamespace.prototype.packet>(packet) in http://vali.dobarkod.hr:7070/socket.io/socket.io.js:
this.socket.packet(packet);
called from line 2014, column 4 in <anonymous function: SocketNamespace.prototype.emit>(name) in http://vali.dobarkod.hr:7070/socket.io/socket.io.js:
return this.packet(packet);
called from line 444, column 8 in <anonymous function>() in http://vali.dobarkod.hr:7070/nowjs/now.js:
socket.emit('rd');
Node says:
debug - served static /socket.io.js
debug - client authorized
info - handshake authorized 428119927451363061
debug - setting request GET /socket.io/1/jsonp-polling/428119927451363061/?t=1310850721185&i=1
debug - setting poll timeout
debug - client authorized for
debug - clearing poll timeout
debug - jsonppolling writing io.j[1]("1::");
debug - set close timeout for client 428119927451363061
debug - setting request GET /socket.io/1/jsonp-polling/428119927451363061/?t=1310850721236&i=1
debug - setting poll timeout
debug - clearing poll timeout
debug - jsonppolling writing io.j[1]("5:::{\"name\":\"rd\"}");
debug - set close timeout for client 428119927451363061
debug - discarding transport
debug - cleared close timeout for client 428119927451363061
debug - setting request GET /socket.io/1/jsonp-polling/428119927451363061/?t=1310850721284&i=1
debug - setting poll timeout
debug - discarding transport
debug - cleared close timeout for client 428119927451363061
Thanks for reporting
Addition to the previous comment: this only happens if the html is not served by the node server. In the example, I served the html via apache (:81) and had node on port 7070. When I tried to serve the html from the node server (using paperboy), this error didn't happen.
Question, do you guys have flashsocket enabled? As I'm unable to reproduce it with a plain test suite like.
var io = require('../').listen(8081)
, http = require('http');
var server = http.createServer(function (req, res) {
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<html><body><script src="http://localhost:8081/socket.io/socket.io.js"></script>' +
'<script>'+
'var socket = io.connect("http://localhost:8081");'+
'socket.on("message", function (data) { console.log(data); });'+
'socket.send("mew?");'+
'</script>'+
'</body></html>'
);
});
server.listen(8080);
io.sockets.on('connection', function (socket) {
socket.send('hello');
socket.on('message', function (msg) {
console.log('msg from client', msg);
});
socket.on('disconnect', function (reason) {
console.log('disconnected', reason);
})
});
yes, flashsocket must be enabled
@digitalStyx also tried that, and it just works so there must be some odd edge case here.
Few more questions: which flash versions are you guys using And do you start up your node process with or without 'root' rights?
Also, is this reproducible every time you visit the page in Opera, or just randomly?
Small summaris of the found issues so far:
- Requires Opera
- Requires the html to be loaded from a other port than the socket.io server
- Requires flash to be enabled, as some stack traces go back to:
Error thrown at line 2713, column 4 in <anonymous function: WebSocket.__initialize>() in http://test.beta.pl/js/socketIo.1.js: document.body.appendChild(container);
called from line 2426, column 6 in <anonymous function: Flashsocket.check>(socket) in http://test.beta.pl/js/socketIo.1.js: WebSocket.__initialize();
- Is reproducible under window / linux & mac.
this code gives an error in Opera 11.50 on OSX
io = require('socket.io').listen(1111)
, http = require('http');
io.set('transports', [
'websocket'
, 'flashsocket'
, 'htmlfile'
, 'xhr-polling'
, 'jsonp-polling'
]);
var server = http.createServer(function (req, res) {
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<html><head>' +
'<script src="http://localhost:1111/socket.io/socket.io.js"></script>' +
'<script>var socket = io.connect("http://localhost:1111")</script>'+
'</head><body></body></html>'
);
});
server.listen(8082);
it looks like putting script inside the body tag or disabling the flashsocket eliminates the error
@afanasy
Thanks allot for that test case, the difference with your code & mine is that I actually initialize the connection inside the body, instead of the head of the page. So I hope this is reproducible now!
/me goes testing
Related bug report https://github.com/LearnBoost/socket.io/issues/353 (closed it to prevent duplicates)
Finally! Got it reproducible. Thanks @afanasy :)
So as a temporarily work around / hack you can just move you code from the head to the body. Until we released a fix for this.
great! :)
Fixed, hacked, patched and send as pull request: https://github.com/LearnBoost/socket.io-client/pull/244
Fixed in https://github.com/LearnBoost/socket.io-client/pull/245,
Which just landed in the master of the socket.io-client.