简体   繁体   English

在真实的服务器中运行socket.io服务器,而不是localhost?

[英]Running a socket.io server in a real live server, instead of localhost?

I found a coding from tutorials point to run aa simple socket.io chat server in localhost, I installed necessary environments like nodejs, express, init package.json and I started the server from terminal using command-"node app.js", then I accessed the index page in my localhost it showed the chat page, it is working fine. 我发现教程中的编码指向在localhost中运行一个简单的socket.io聊天服务器,我安装了必要的环境,如nodejs,express,init package.json,我使用命令“node app.js”从终端启动服务器,然后我访问的索引页在我的本地主机它显示聊天页面,它工作正常。 But the thing is I want to use this is in a live server for my office, to chat within the office. 但事情是我想用这个在我的办公室的实时服务器,在办公室内聊天。 Is this code is enough for that. 这段代码是否足够。 I am new to this socket.io and nodejs. 我是这个socket.io和nodejs的新手。 My office has live server for hosting their website, this code opens and listens to port 3000. It will be highly helpful if you could tell me how to run this in a real server. 我的办公室有用于托管其网站的实时服务器,此代码打开并侦听端口3000.如果您能告诉我如何在真实服务器中运行它,将会非常有用。

Index.html 的index.html

  <!DOCTYPE html>
<html>
   <head>
      <title>Hello world</title>
   </head>

   <script src = "/socket.io/socket.io.js"></script>
   <script>
      var socket = io();
      function setUsername() {
         socket.emit('setUsername', document.getElementById('name').value);
      };
      var user;
      socket.on('userExists', function(data) {
         document.getElementById('error-container').innerHTML = data;
      });
      socket.on('userSet', function(data) {
         user = data.username;
         document.body.innerHTML = '<input type = "text" id = "message">\
         <button type = "button" name = "button" onclick = "sendMessage()">Send</button>\
         <div id = "message-container"></div>';
      });
      function sendMessage() {
         var msg = document.getElementById('message').value;
         if(msg) {
            socket.emit('msg', {message: msg, user: user});
         }
      }
      socket.on('newmsg', function(data) {
         if(user) {
            document.getElementById('message-container').innerHTML += '<div><b>' + 
               data.user + '</b>: ' + data.message + '</div>'
         }
      })
   </script>

   <body>
      <div id = "error-container"></div>
      <input id = "name" type = "text" name = "name" value = "" 
         placeholder = "Enter your name!">
      <button type = "button" name = "button" onclick = "setUsername()">
         Let me chat!
      </button>
   </body>
</html>

app.js Server app.js服务器

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

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

  users = [];
 io.on('connection', function(socket) {
 console.log('A user connected');
 socket.on('setUsername', function(data) {
  console.log(data);

  if(users.indexOf(data) > -1) {
     socket.emit('userExists', data + ' username is taken! Try some              
  } else {
     users.push(data);
     socket.emit('userSet', {username: data});
  }
 });

  socket.on('msg', function(data) {
  //Send message to everyone
  io.sockets.emit('newmsg', data);
  })
 });

http.listen(3000, function() {
console.log('listening on localhost:3000');
});

You can solve your problem through nginix( A reverse proxy server). 您可以通过nginix(反向代理服务器)解决您的问题。 Nginx have .conf file which contains the server realted configuration. Nginx有.conf文件,其中包含服务器实现的配置。

server { listen 3000; server_name io.yourhost.com; }

To run: 跑步:

Sudo service nginx start

It will start your server on given IP or Domain name. 它将在给定的IP或域名上启动您的服务器。

Change the declaration variable socket by var socket = io(Server IP + ':port'); 通过var socket = io(Server IP + ':port');更改声明变量socket var socket = io(Server IP + ':port');

Example: 例:

var socket = io('127.0.0.1:3000);

I using socket.io version 2.0.2 我使用socket.io版本2.0.2

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

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