Answer the question
In order to leave comments, you need to log in
Why does this error occur with sockets?
here is the server code
var http = require('http');
var fs = require('fs');
var server = http.createServer(app);
server.listen(config.get('port'), function(){
log.info('Express server listening on port ' + config.get('port'));
});
var io = require('socket.io').listen(server);
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('my other event', function (data) {
console.log(data);
});
});
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io('http://localhost');
socket.on('news',function (data) {
console.log(data);
socket.emit('my other event',{my:'data'});
});
</script>
/usr/bin/nodejs /var/www/html/chatnode/app.js
info: [chatnode/app.js] Express server listening on port 3000
GET http://localhost/socket.io/?EIO=3&transport=polling&t=LRcrDTD
404 Not Found 0ms
Connection : Keep-Alive
Content-Length : 283
Content-Type : text/html; charset=iso-8859-1
Date : Thu, 01 Sep 2016 17:38:43 GMT
Keep-Alive : timeout=5, max=98
Server Apache/2.4.18 (Ubuntu)
Accept : text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding : gzip, deflate
Accept-Language : en-US,en;q=0.5
Cookie : sid=s%3AO5ULQ9g2P90WX7Ypsh5e9ELy.%2FgllbE05X1GwmsSw%2FcId3VbnFPYFgnhZ%2BkJs6i54Jok
DNT : 1
Host : localhost
Origin : http://localhost:3000
Referer : http://localhost:3000/chat
User-Agent : Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:48.0) Gecko/20100101 Firefox/48.0
EIO 3
t LRcrDTD
transport polling
null
Answer the question
In order to leave comments, you need to log in
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io('http://localhost:3000'); // <-- хост:порт
socket.on('news',function (data) {
console.log(data);
socket.emit('my other event',{my:'data'});
});
</script>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question