Reputation: 1700
I keep getting this error in my browser's console so I think it is an angular + socket.io problem:
GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=LF3gkk7 net::ERR_CONNECTION_REFUSED
Eventho I followed advices which told to change angular var io
like:
ctrl.socketIo = io();
ctrl.socketIo = io.connect();
ctrl.socketIo = io.connect('http://localhost:3001');
none of those worked!
This is how my Angular controller looks:
angular.module("app")
.controller("chatController", [
'userService',
function (userService) {
var ctrl = this;
ctrl.messages = [];
ctrl.socketIo = io();
ctrl.userName = userService.name;
ctrl.submit = function () {
ctrl.socketIo.emit("chat_message", ctrl.inputMessage);
ctrl.messages.push(ctrl.inputMessage);
ctrl.inputMessage = null;
};
ctrl.socketIo.on("update_clients", function (msg) {
ctrl.messages.push(msg);
});
}]
);
My index.html path are pointing to the dirs:
<!doctype html>
<html>
<head>
//boilerplate...
</head>
<body ng-app="app">
//boilerplate...
<script src="/bower_components/socket.io-client/socket.io.js"></script>
//boilerplate...
</body>
</html>
As you can see in my dir structure path is pointing correctly to socket.io.js:
I'm using Digital ocean VPS, and when I run netstat -a -p
there does NOT seem that PORT 3000 is being used.
One service in my app was trying to connect to localhost:3000 so I erased that part since I don't need socket there now I'm getting:
'use strict';
var express = require("express");
var app = express();
var http = require("http");
var server = http.createServer(app);
var io = require("socket.io")(http);
//use environment var PORT or 3001
var portNum = 3001;
//route handler
app.get("/", function (req, res) {
res.sendFile("./index.html", {root: "./client/"});
});
app.use(express.static('./client'));
io.on("connection", function (socket) {
socket.on("chat_message", function (msg) {
socket.broadcast.emit('update_clients', msg);
});
});
server.listen(portNum, function () {
console.log("listening on port: " + portNum);
});
Upvotes: 1
Views: 1321
Reputation: 9022
You are getting 404
because your socket.io module was not properly initialized.
Try the following code.
'use strict';
var express = require("express");
var app = express();
var http = require("http").Server(app);
var io = require("socket.io")(http);
//use environment var PORT or 3001
var portNum = 3001;
//route handler
app.get("/", function (req, res) {
res.sendFile("./index.html", {root: "./client/"});
});
app.use(express.static('./client'));
io.on("connection", function (socket) {
socket.on("chat_message", function (msg) {
socket.broadcast.emit('update_clients', msg);
});
});
http.listen(portNum, function () {
console.log("listening on port: " + portNum);
});
Upvotes: 1