Nodejs &socket io:警告 - 引发错误:错误:听 EADDRINUSE [英] Nodejs & socket io : warn - error raised: Error: listen EADDRINUSE

查看:18
本文介绍了Nodejs &socket io:警告 - 引发错误:错误:听 EADDRINUSE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 socket.io 使用 Node.js

I am giving a try to Node.js with socket.io

现在这是我的场景,我是 ubuntu 12.04 用户,我在桌面上有文件夹 pp

Now here is my scenario i am ubuntu 12.04 user and i have folder pp on desktop

在里面我放了服务器文件,即 app.js

inside that i am putted server file i.e app.js

这里是内容

var fs = require('fs')
    , http = require('http')
    , socketio = require('socket.io');

var server = http.createServer(function(req, res) {
    res.writeHead(200, { 'Content-type': 'text/html'});
    res.end(fs.readFileSync(__dirname + '/index.html'));
}).listen(8080, function() {
    console.log('Listening at: http://localhost:8080');
});

socketio.listen(server).on('connection', function (socket) {
    socket.on('message', function (msg) {
        console.log('Message Received: ', msg);
        socket.broadcast.emit('message', msg);
    });
});

现在在同一个文件夹中,我有另一个文件 index.html

Now in the same folder i have another file index.html like

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <script>
        $(function(){
            var iosocket = io.connect();

            iosocket.on('connect', function () {
                $('#incomingChatMessages').append($('<li>Connected</li>'));

                iosocket.on('message', function(message) {
                    $('#incomingChatMessages').append($('<li></li>').text(message));
                });
                iosocket.on('disconnect', function() {
                    $('#incomingChatMessages').append('<li>Disconnected</li>');
                });
            });

            $('#outgoingChatMessage').keypress(function(event) {
                if(event.which == 13) {
                    event.preventDefault();
                    iosocket.send($('#outgoingChatMessage').val());
                    $('#incomingChatMessages').append($('<li></li>').text($('#outgoingChatMessage').val()));
                    $('#outgoingChatMessage').val('');
                }
            });
        });
    </script>
</head>
<body>
Incoming Chat: <ul id="incomingChatMessages"></ul>


<input type="text" id="outgoingChatMessage">
</body>

当我尝试使用像

 node app.js 

我收到错误

 warn  - error raised: Error: listen EADDRINUSE

我查看了一些文档,发现端口很忙,因此重新启动了系统,但我仍然遇到相同的错误.

I go through some doc and found that port are busy so restarted the system but still i am getting the same error .

请告诉我我可能做错了什么.

Please tell me what might i am doing wrong .

推荐答案

尝试另一个端口.8080 通常被 tomcat 使用,例如

Try another port. 8080 is usually used by tomcat e.g.

使用 netstat -a -v 可以知道当前使用的是哪个端口.

Use netstat -a -v to know which port are currently used.

这篇关于Nodejs &amp;socket io:警告 - 引发错误:错误:听 EADDRINUSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆