【发布时间】:2015-04-23 16:52:12
【问题描述】:
我想将 nw.js 用作独立的套接字客户端和服务器。我正在尝试将来自 socket.io 网站的 sample socket.io 聊天应用程序放入 nw.js 应用程序中。但是当我启动 nw.js 时出现以下错误:
[5591:0222/143044:ERROR:nw_shell.cc(325)] Error: listen EADDRINUSE :::3000
at Object.exports._errnoException (util.js:734:11)
at exports._exceptionWithHostPort (util.js:757:20)
at Server._listen2 (net.js:1153:14)
at listen (net.js:1179:10)
at Server.listen (net.js:1266:5)
at Object.<anonymous> (/Users/xxxx/nwjs/chat-example/index.js:23:6)
at Module._compile (module.js:451:26)
at Object.Module._extensions..js (module.js:469:10)
at Module.load (module.js:346:32)
at Function.Module._load (module.js:301:12)
我尝试了多个端口,但都没有成功。 这是我用于服务器的代码,与 socket.io 指南提供的代码基本相同。
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('chat message', function(msg){
console.log('message: ' + msg);
io.emit('chat message', msg);
});
});
http.listen( 3000, function(){
console.log('listening on *:3000');
});
这是我的 nw.js 的 package.json
{
"name": "socket-chat-example",
"version": "0.0.1",
"description": "my first socket.io app",
"dependencies": {
"express": "^4.10.2",
"socket.io": "^1.3.4"
},
"main": "index.html",
"node-main": "index.js"
}
我的 nwjs 版本:v0.12.0-alpha3-osx-x64
【问题讨论】:
-
你有一些其他的应用程序监听 3000 端口
-
@AlexeyTen 但我随机尝试了其他端口,但它们都不起作用(例如 3001、3023、8883、8000)
标签: node.js express socket.io node-webkit nw.js