node.js拒绝连接吗?

时间:2018-11-18 16:59:04

标签: node.js socket.io

我有一些正在运行的名为app.js的node.js代码,运行时会出现错误,提示连接被拒绝?

它在plesk上运行,因此我无法对其进行调试。我也添加了package.json,但是也许我缺少了什么?

package.json:

{
  "name": "socket-example",
  "version": "0.0.1",
  "description": "test socket.io app",
  "dependencies": {
    "socket.io": "^1.7.3"
  },
  "scripts": {
    "start": "node app.js"
  }
}

app.js:

var http = require('http');
var fs = require('fs');


// Loading the index file . html displayed to the client
var server = http.createServer(function(req, res) {
    fs.readFile('./index.html', 'utf-8', function(error, content) {
        res.writeHead(200, {"Content-Type": "text/html"});
        res.end(content);
    });
});

// Loading socket.io
var io = require('socket.io').listen(server);

// When a client connects, we note it in the console
io.sockets.on('connection', function (socket) {
    console.log('A client is connected!');
});


server.listen(8080);

index.html:

<body>
    <h1>Communicating with socket.io!</h1>

    <p><input type="button" value="Poke the server" id="poke" /></p>


    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <script>
        var socket = io.connect('http://localhost:8080');

        // The visitor is asked for their username...
        var username = prompt('What\'s your username?');

        // It's sent with the signal "little_newbie" (to differentiate it from "message")
        socket.emit('little_newbie', username);

        // A dialog box is displayed when the server sends us a "message"
        socket.on('message', function(message) {
            alert('The server has a message for you: ' + message);
        })

        // When the button is clicked, a "message" is sent to the server
        $('#poke').click(function () {
            socket.emit('message', 'Hi server, how are you?');
        })
    </script>
</body>

2 个答案:

答案 0 :(得分:0)

尝试像这样更改所有代码,因为这将解决您的问题
app.js

var express = require('express');

var app = express();

app.set("view engine","html");


var http = require('http');

//making express server for routes
var server = http.Server(app);




// Loading socket.io
var io = require('socket.io')(server);

//index route
//loaded when the website is loaded root route.
app.get('/',function(req,res){
	//specify the path to the directory (assuming both are in the same directory)
	res.sendFile(__dirname + '/index.html');
})

// When a client connects, we note it in the console
io.sockets.on('connection', function (socket) {
    console.log('A client is connected!');

    //message of client side
    socket.on('little_newbie',function(message){
    	//give the username in terminal of the connected client
    	console.log(message);
    	//send a hello to the client from the server.
    	io.sockets.emit('message',"Hello "+message);
    })
    //button poke mesage response
    socket.on('message',function(message){
    	//print the hello message from the server
    	console.log(message)

    	//after this you can send a response back to the client as in the above case
    	io.sockets.emit('poke',"Hi I am fine ");
    })

});

server.listen(8080);
<!DOCTYPE html>
<html>
    <head>
    <meta charset="utf-8" />
    <title>Socket.io</title>
</head>

<body>
    <h1>Communicating with socket.io!</h1>

    <p><input type="button" value="Poke the server" id="poke" /></p>


    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <script>
        var socket = io.connect('/');

        // The visitor is asked for their username...
        var username = prompt('What\'s your username?');

        // It's sent with the signal "little_newbie" (to differentiate it from "message")
        socket.emit('little_newbie', username);

        // A dialog box is displayed when the server sends us a "message"
        socket.on('message', function(message) {
            alert('The server has a message for you: ' + message);
        })

        // When the button is clicked, a "message" is sent to the server
        $('#poke').click(function () {
            socket.emit('message', 'Hi server, how are you?');
        })

        socket.on('poke',function(message){
            alert(message);
        })
    </script>
</body>

{
  "name": "socket-example",
  "version": "0.0.1",
  "description": "test socket.io app",
  "dependencies": {
    "express": "^4.16.4",
    "socket.io": "^1.7.3"
  },
  "scripts": {
    "start": "node app.js"
  }
}

答案 1 :(得分:0)

没有路由配置,当您尝试从浏览器ping时,它将无法连接。就像少了URI一样。尝试制作一条路线,然后从浏览器中插入路线。