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

时间:2018-05-29 13:11:14

标签: node.js express socket.io

我正在尝试使用我的项目文件夹(位于Linux(Ubuntu)服务器上)中的节点js express和socket.io创建套接字连接。安装nodejs后,npm,socketio和express我无法创建套接字连接。试图运行节点app.js但没有输出。按照教程http://www.programwitherik.com/getting-started-with-socket-io-node-js-and-express/完成安装。我包含了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是示例地址,其中socket-app是包含app.js和index.html文件的文件夹。还有如何在浏览器上运行它,因为每个教程都使用localhost运行它。安装在本地计算机上时,类似的代码可以正常工作。

1 个答案:

答案 0 :(得分:0)

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