I'm trying to get very first example from socket.io home page working in Chrome(19.0.1048.46) but I get the error:
"XMLHttpRequest cannot load http:// localhost:8080/socket.io/1/?t=1337156198176. Origin null is not allowed by Access-Control-Allow-Origin."
Server code:
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(8080);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
Client code:
<script src="socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:8080');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
What is wrong with this code ?
I believe you are opening the index.html file directly in the browser, is that the case?
With file:// protocol no origin is set to the XMLHttpRequest, so the browser will raise that security exception if the server doesn't set the Access-Control-Allow-Origin header enabling CORS
Navigate to http://localhost:8080/ instead, the handler specified in the code should render the index.html page correctly
Related
Good day, I'm trying to use socket.io i facing some problem with 404 not found. I'm using :84 as my xampp port
Folder structure
--tests
--node_modules
--app.js
--index.html
Here is my html content
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.3/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:84/test/');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
and this app.js
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(8080);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
I'm bit confuse about setting this part
var socket = io.connect('http://localhost:84/test/');
and this part
app.listen(8080);
I'm using windows 7 64 bit and i'm using xampp to run it. When i run it i get 404 error like this
"NetworkError: 404 Not Found - http://localhost:84/socket.io/?EIO=3&transport=polling&t=Ll8amII"
how can i fix it? sorry for my english and thanks in advance.
You are connecting to the wrong port.
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io('http://localhost:8080'); // CHANGE HERE
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', {my: 'data' });
});
</script>
XAMPP has nothing to do with express.
I'm using PhantomJS 2.0 and trying to test socket.io between PhantomJS and NodeJS. It works but it takes alot of time for each connection (about 1000ms). I have tried on Chrome and it takes only 1-2ms. It's seems that socket.io in page.evaluate of PhantomJS is not socket.io!
Here is my testing code using CasperJS:
var casper = require('casper').create({
clientScripts:["socket.io.js"]
});
casper.start('http://localhost:8080');
casper.page.onConsoleMessage = function(msg) {
console.log(msg);
};
};
casper.then(function(){
this.evaluate(function(){
var socket = io.connect('http://localhost:8080');
socket.on('news', function (data) {
socket.emit('senddata', { time: new Date().getTime() });
});
})
})
casper.wait(5000);
casper.run();
On NodeJS server I use this code:
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');
app.listen(8080);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('senddata', function (data) {
console.log(new Date().getTime()-data.time);
});
});
The result of time measuring is always >= 1000ms. I need to tranfer data in serveral ms. Can anyone help me.
Thanks
i use code from examples of socket.io site and has some problem
My server code (on debian 192.168.5.200)
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);
server.listen(1337);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
My client code (index.html)
<script src="http://{host ip}:1337/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://{host ip}:1337');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
i start node server
open in browser http://{host ip}:1337
and... got 404 on socket.io connection
it try to get "/api/1/?t=..." url and got answer by express "Cannot get /api/1/?t=..." with 404 error
Please help me (
Try to change your client code to:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect();
socket.on("connect", function() {
socket.on('news', function (data) {
socket.emit('my other event', { my: 'data' });
});
});
</script>
Also, in order to be able to go to /api/1... you need to register the corresponding app.get, e.g. as app.get("/api/*", ..., which would handle all the connections to /api/.... Otherwise it is expected that you will be getting the 404 error.
I am new to Node and Socket.io and attempting to get the below code running. It is designed to service WebSocket requests, however I would like to amend it to also serve static content such as index.html.
My web socket code is as follows:
var http = require("http"),
sys = require("sys"),
io = require("socket.io"),
GlobTrie = require("glob-trie.js");
var Brokaw = {};
Brokaw.Server = function(port) {
var self = this;
// setup the basic HTTP server -- socket.io will wrap this
var server = http.createServer(function(req, res) {
// We have to respond with something, otherwise the connections hang :(
res.writeHead(404);
res.close();
});
server.listen(port);
this._io = io.listen(server, { "resource": "brokaw" });
this._trie = new GlobTrie();
this._io.on("connection", function(conn) {
new Brokaw.Server.Client(conn, self);
});
};
new Brokaw.Server(8080);
And my code to serve the index.html is as follows:
// Hardcode *all* HTTP requests to this server to serve up index.html
fs.readFile(
__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
}
);
Can anyone advise on how to integrate the two i.e. amend the top code to serve my index.html file also?
Appreciate any comments, and I have been struggling for many hours!
Regards, Ben.
There is an example, taken from the official Socket.IO homepage:
Server-side:
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(80);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
Client-side:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
I'm trying an example of WebSocket to develop a simple chat.
server.js:
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(8080);
function handler (req, res) {
fs.readFile(__dirname + '/test.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
and test.html:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('connect', function() {
alert('<li>Connected to the server.</li>');
});
socket.on('message', function(message) {
alert(message);
});
socket.on('disconnect', function() {
alert('<li>Disconnected from the server.</li>');
});
function sendF(){
var message = "Test";
socket.send(message);
alert('Test Send');
}
In test.html I have also a simple button that onClick call sendF.
If I try it I see on my browser an alert when I CONNECT, SEND, and DISCONNCT, and if I check in console, I see the message.
But I cannot receive the same message in response from server, to show it in my browser! I think socket.on('message'... is not working for me!
Your server.js is missing event listeners. It's also missing where you are doing the send of the message to be displayed in the browser.
io.sockets.on('connection', function (socket) {
console.log('user connected');
socket.send('hello world');
socket.on('disconnect', function () {
console.log('user disconnected.');
});
socket.on('message', function (data) {
console.log(data);
});
});