简体   繁体   中英

Trying to Implement Node.js and Socket.io on dedicated server

Recently, I have been experimenting with Node.js and Socket.io. Successfully, I have been able to implement these but only on localhost. Now I would like use this functionality on my dedicated server. I am hosting my server from my own home and I cannot get node and socket to run outside of localhost. The importance of this is so that I can use two different computers while testing out my site. Here is my code as follows:

app.js:

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

server.listen(80);

app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

index.html:

    <script src="http://localhost:8080/socket.io/socket.io.js"></script>
    <script>
      var socket = io.connect('http://localhost:8080');
      socket.on('news', function (data) {
        console.log(data);
        socket.emit('my other event', { my: 'data' });
      });

</script>

These are samples that were taken directly off the socket.io site. Now it works on localhost, but not externally. I cannot use it on another computer. I was advised to change

var socket = io.connect('http://localhost:8080');
to
 Uncaught ReferenceError: io is not defined mydomain.com/:3
    GET http://mydomain.com/socket.io/socket.io.js  mydomain.com/:1
, but that makes the browser throw the following errors :

  Uncaught ReferenceError: io is not defined mydomain.com/:3 GET http://mydomain.com/socket.io/socket.io.js mydomain.com/:1 

even on my main server computer. Im using OSX server with a mac mini btw.

Any advice would be appreciated:

It looks like you're listening on port 80 instead of 8080, and you will need to point your script block to your server, so try

<script src="http://localhost:8080/socket.io/socket.io.js"></script>

needs to be

<script src="http://mydomain.com:80/socket.io/socket.io.js"></script>

or better yet

<script src="/socket.io/socket.io.js"></script>

If that doesn't work, make sure you check for any errors under the network tab of your debugger window (presuming you're using chrome) to make sure you're loading that socket.io.js file correctly.

You don't need to specify server address if it's the same where the js was loaded:

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect();
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>

but you need to make http://mydomain.com/socket.io/socket.io.js available for you browser.

Ok. try to run this code:

filename: package.json

{
"name": "test-socketio"
, "version": "0.0.1"
, "private": true
, "dependencies": {
    "socket.io": "*"
},
"engines": {
    "node": "0.8.x"
    ,"npm" : "*"
}

}

filename: app.js

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

server.listen(80);

app.get('/', function (req, res) {
    res.sendfile(__dirname + '/index.html'); 
});

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log("Recevied: 'my other event'");
    console.log(data);
  });
});

filename: index.html

<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect();
    socket.on('news', function (data) {
        console.log("Received news event");
        console.log(data);
        socket.emit('my other event', { my: 'data' });
    });
</script>

Then run in the code directory:

npm install

and run:

sudo node app.js

Please remember that you need to have the root rights to start listening on port 80.

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