简体   繁体   中英

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. After installing nodejs, npm, socketio and express I am unable to create socket connection. Tried to run node app.js but no output comes. Installation is done following the tutorial 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
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. 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 "); instead of io.connect();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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