连接到服务器通过 Socket.io 客户端

huangapple go评论70阅读模式
英文:

connection to server through socket io client

问题

我在Node.js中使用express和socket.io创建了一个服务器应用程序。我可以通过socket.io客户端从浏览器连接到服务器应用程序。我希望我的应用程序可以在家庭网络内访问,并且我的同事可以使用我的IPv4地址连接到我的应用程序,但是socket.io连接不起作用。我在socket.io连接中手动设置了我的IPv4地址。

Node.js代码:

var express = require("express");
var bodyParser = require("body-parser");
var app = express();
var http_server = require('http').createServer(app);
var cors = require("cors")
var io = require('socket.io')(http_server)

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

    client.on('disconnect', () => {
        console.log("Client disconnected..");
    });
});

var server = http_server.listen(3000, function () {
    console.log("app running on port.", server.address().port);
});

HTML代码:

<script src="/socket.io/socket.io.js"></script>
<script>
   var socket = io.connect('http://localhost:3000');
      socket.on('connect', function() {
          console.log("server connected..");
      });
</script>

如何在HTML代码中指定连接到我的服务器..??

我尝试了谷歌和来自stackoverflow的各种响应,但没有帮助。

英文:

I created an server application in Node js using express, socket.io. I am able to connect server application from browser via socket.io client. I want my application to access within the home network and my colleagues are able to connect to my application with my IPV4 address but socket io connection is not working. I manuaaly set my IPV4 address in there socket io connection.

Node js:

var express = require(&quot;express&quot;);
var bodyParser = require(&quot;body-parser&quot;);
var app = express();
var http_server = require(&#39;http&#39;).createServer(app);
var cors = require(&quot;cors&quot;)
var io = require(&#39;socket.io&#39;)(http_server)


io.on(&#39;connection&#39;, function(client) {
    console.log(&#39;Client connected...&#39;);

    client.on(&#39;disconnect&#39;, () =&gt; {
        console.log(&quot;Client disconnected..&quot;);
    });
});


var server = http_server.listen(3000, function () {
    console.log(&quot;app running on port.&quot;, server.address().port);
});

HTML code:

&lt;script src=&quot;/socket.io/socket.io.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
   var socket = io.connect(&#39;http://localhost:3000&#39;);
      socket.on(&#39;connect&#39;, function() {
          console.log(&quot;server connected..&quot;);
      });
&lt;/script&gt;

How to mention in Html code to connect to my server..??

I tried google and various responses from stackoverflow but nothing helpful..

答案1

得分: 0

我找到了答案..

HTML 代码:

<script src="/socket.io/socket.io.js"></script>
var ipAddress = 'http://你的IPV4地址:3000';
<script>
   var socket = io.connect(ipAddress);
      socket.on('connect', function() {
          console.log("服务器已连接..");
      });
</script>

我将 ipAddresslocalhost 更改为我的 IPV4地址。一旦重新启动服务器,我的同事就可以在他们的计算机上访问我的应用程序。

英文:

I found the answer..

HTML Code:

&lt;script src=&quot;/socket.io/socket.io.js&quot;&gt;&lt;/script&gt;
var ipAddress = &#39;http://localhost:3000&#39;;
&lt;script&gt;
   var socket = io.connect(ipAddress);
      socket.on(&#39;connect&#39;, function() {
          console.log(&quot;server connected..&quot;);
      });
&lt;/script&gt;

I changed the ipAddress from localhost to my IPV4 address. It works fine once i restart server and my colleagues can access my application in their computer.

huangapple
  • 本文由 发表于 2020年1月6日 18:59:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定