简体   繁体   English

在Linux服务器上的项目文件夹中使用node js express和socket.io创建套接字连接时出错

[英]Error creating socket connection using node js express and socket.io in my project folder on linux server

I am trying to create socket connection using node js express and socket.io in my project folder which is on Linux(Ubuntu) server. 我正在尝试在Linux(Ubuntu)服务器上的项目文件夹中使用node js express和socket.io创建套接字连接。 After installing nodejs, npm, socketio and express I am unable to create socket connection. 安装nodejs,npm,socketio和express之后,我无法创建套接字连接。 Tried to run node app.js but no output comes. 试图运行节点app.js,但没有输出。 Installation is done following the tutorial http://www.programwitherik.com/getting-started-with-socket-io-node-js-and-express/ . 按照教程http://www.programwitherik.com/getting-started-with-socket-io-node-js-and-express/进行安装。 I am including code for app.js and index.html 我包括app.js和index.html的代码

//app.js
var express = require('express');  
var app = express();  
var server = require('http').createServer(app);  
var io = require('socket.io').listen(server);

io.on('connection', function(client) {  
    console.log('Client connected...');

    client.on('join', function(data) {
        console.log(data);
    });
});

app.use(express.static(__dirname + '/node_modules'));  
app.get('/', function(req, res,next) {  
    res.sendFile(__dirname + '/index.html');
});

server.listen(8000);

//index.html
<!doctype html>  
<html lang="en">  
    <head>
    <script src="http://example.abcd.net/socket-app/jquery/dist/jquery.js"></script>
        <script src="http://example.abcd.net:8000/socket.io/socket.io.js"></script>
    </head>
    <body>
        <h1>Hello World!</h1>
        <div id="future"></div>
        <form id="form" id="chat_form">
            <input id="chat_input" type="text">
            <input type="submit" value="Send">
        </form>

    </body>
</html>  
<script>  
 var socket = io.connect();
 socket.on('connect', function(data) {
    socket.emit('join', 'Hello World from client');
 });
</script>

example.abcd.net is the sample address in which socket-app is the folder which contains app.js and index.html file. example.abcd.net是示例地址,其中socket-app是包含app.js和index.html文件的文件夹。 Also how to run it on browser as every tutorial runs it using localhost. 以及每个教程使用本地主机运行它时如何在浏览器上运行它。 Similar code works fine when installed on local machine. 将类似的代码安装在本地计算机上时可以正常工作。

Try using io.connect(" http://example.abcd.net:8000 "); 尝试使用io.connect(“ http://example.abcd.net:8000 ”); instead of io.connect(); 而不是io.connect();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM