У меня есть код node.js, который я запускаю, который называется app.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.info('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>





Попробуйте изменить весь код таким образом, поскольку это решит ваши проблемы. 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.info('A client is connected!');
//message of client side
socket.on('little_newbie',function(message){
//give the username in terminal of the connected client
console.info(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.info(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"
}
}Конфигурации маршрутов не было, и когда вы пытаетесь пинговать из браузера, он не подключается. точно так же, как означает меньше URI. Попробуйте проложить маршрут, а затем рассчитать маршрут из браузера.
Спасибо за это, но как мне это сделать? Я новичок в node.js, и этот код взят с учебного сайта и ничего не упоминает о маршрутах? Спасибо за вашу помощь
Комментарии не подлежат расширенному обсуждению; этот разговор был переехал в чат.