节点 + AngularJs + Socket.io: net::ERR_CONNECTION_REFUSED
Node + AngularJs + Socket.io: net::ERR_CONNECTION_REFUSED
我在浏览器的控制台中不断收到此错误,所以我认为这是一个 angular + socket.io 问题:
GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=LF3gkk7 net::ERR_CONNECTION_REFUSED
Eventho 我听从了建议改变 angular var io
像:
ctrl.socketIo = io();
ctrl.socketIo = io.connect();
ctrl.socketIo = io.connect('http://localhost:3001');
none 个工作!
这是我的 Angular 控制器的样子:
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);
});
}]
);
我的 index.html 路径指向目录:
<!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>
正如您在我的目录结构路径中看到的那样正确指向 socket.io.js:
更新
我正在使用 Digital ocean VPS,当我 运行 netstat -a -p
时,似乎没有使用 PORT 3000。
更新 2:
我的应用程序中的一项服务正在尝试连接到 localhost:3000,所以我删除了那部分,因为我不需要那里的套接字,现在我得到:
更新 3 后端代码:
'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);
});
你得到 404
因为你的 socket.io 模块没有正确初始化。
试试下面的代码。
'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);
});
我在浏览器的控制台中不断收到此错误,所以我认为这是一个 angular + socket.io 问题:
GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=LF3gkk7 net::ERR_CONNECTION_REFUSED
Eventho 我听从了建议改变 angular var io
像:
ctrl.socketIo = io();
ctrl.socketIo = io.connect();
ctrl.socketIo = io.connect('http://localhost:3001');
none 个工作!
这是我的 Angular 控制器的样子:
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);
});
}]
);
我的 index.html 路径指向目录:
<!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>
正如您在我的目录结构路径中看到的那样正确指向 socket.io.js:
更新
我正在使用 Digital ocean VPS,当我 运行 netstat -a -p
时,似乎没有使用 PORT 3000。
更新 2:
我的应用程序中的一项服务正在尝试连接到 localhost:3000,所以我删除了那部分,因为我不需要那里的套接字,现在我得到:
更新 3 后端代码:
'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);
});
你得到 404
因为你的 socket.io 模块没有正确初始化。
试试下面的代码。
'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);
});