英文:
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("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 code:
<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>
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>
我将 ipAddress
从 localhost
更改为我的 IPV4地址
。一旦重新启动服务器,我的同事就可以在他们的计算机上访问我的应用程序。
英文:
I found the answer..
HTML Code:
<script src="/socket.io/socket.io.js"></script>
var ipAddress = 'http://localhost:3000';
<script>
var socket = io.connect(ipAddress);
socket.on('connect', function() {
console.log("server connected..");
});
</script>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论