I have a problem with Nodejs and my js code.
When I run the Nodejs in console server does not show any problems, but all time I get an error in the console from Chrome:
GET http://localhost/socket.io/?EIO=3&transport=polling&t=LjIvLGU 404 (Not Found)
index.html
<head>
<title>test</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
</head>
script.js
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io').listen(server);
users = [];
connections = [];
server.listen(process.env.PORT || 3000);
console.log("server up");
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.php');
});
io.sockets.on('connection', function (socket) {
socket.on('subscribe', function (data) {
socket.join(data.room);
});
socket.on('arriv', function (data) {
console.log(data);
io.sockets.in('invitor').emit('send message',data);
});
socket.on('unsubscribe', function (data) {
socket.leave(data.room);
});
});
user.js
$(function () {
var cid = $.session.get('company');
console.log(cid);
var socket = io.connect();
socket.emit("subscribe", {room: "invitor"});
socket.on("send message", function (data){
$('#info').append("" + data.gid + "</br>");
});
$(document).on('click', '.123', function () {
var gid = $(this).attr('id');
socket.emit('arriv', {gid: gid});
});
});
These are query parameters that the socket.io client sends to the socket.io server as part of the initial connection request. EIO=3 , I believe, is the version number of the engine.io sub-system in socket.io. If the server is not compatible with this version number, it will likely fail the attempt to connect.
js) and the Socket.IO client (browser, Node. js, or another programming language) is established with a WebSocket connection whenever possible, and will use HTTP long-polling as fallback.
It seems that you are trying to connect your socket somewhere else over localhost without port 3000. If i test your code snippets everything is OK. Your error message shows me that port 3000 is missing " GET http://localhost/socket.io/?EIO=3&transport=polling&t=LjIvLGU 404 (Not Found)"
.
You can try to connect manually if you change your io.connect to var socket = io.connect('http://localhost:3000');
" GET http://localhost/socket.io/?EIO=3&transport=polling&t=LjIvLGU 404 (Not Found)" means you have socket.io is not listening to your intended server. My mistake was that I instantiated the wrong server object.
In app.js file
var server = app.listen(config.dev.port, () => {
console.log("Listening ..");
});
var io = require('socket.io').listen(server);
After a lot of searching, this works:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io('http://localhost:3000/');
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With