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.
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 have the following nodejs web socket server
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(8000);
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
console.log("Listening on 8000");
When trying to connect using wscat
wscat -c ws://localhost:8000/
I get the following error
error: Error: socket hang up
It works just fine in browser with the following javascript
var socket = io.connect('http://localhost:8000');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
Help is appreciated!
I suggest that you try connecting to endpoint while specifying transport protocol. Like this:
wscat -c ws://localhost:3000/socket.io/\?transport=websocket
This works for my case.
Here is my server
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
Here is my client index.html
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:80');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
Here are my commaneds
node server.js
and on browser I am hitting URL localhost:80/index.html
after hitting above URL I am getting message
Welcome to socket.io.
I am using nodeJS v0.10.9
socket.io provides a socket.io server, not a web server. So index.html in the context of socket.io doesn't exist.
Instead, you could use Express to provide a basic web server, combined with socket.io to provide messaging:
// server.js
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
app.use(express.static(__dirname + '/public'));
server.listen(80);
// ./public/index.html
<!doctype html>
<html>
<head>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect(); // let socket.io autodiscover the server
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
Whenever I execute my code, I get message at console that it's connected than after that that nothing happens.
Here is the code:
server-side
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);
server.listen(80);
app.get('/', function (req, res) {
res.sendfile('index.html');
});
io.sockets.on('connection', function (socket) {
console.log('m connected');
socket.emit('news', { hello: 'm joooon ' });
socket.on('my other event', function (data) {
console.log(data);
});
});
client-side
<!DOCTYPE html>
<html>
<head>
<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>
</head>
<body>
<div id="lo">magudi</div>
</body></html>
On the client side, your not listening to the port.
var socket = io.connect('http://localhost:80');
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