开发者

Socket.io/node.js on local network?

开发者 https://www.devze.com 2023-04-05 23:19 出处:网络
I have two machines on the same network say at 192.168.1.2 and 192.168.1.3. 192.168.1.2 = server/dev pc

I have two machines on the same network say at 192.168.1.2 and 192.168.1.3.

192.168.1.2 = server/dev pc

192.168.1.3 = client/browser pc

So on the server/dev pc I have a socket.io/http server running on port 82

On the client server I'm using chrome as the browser

The server is hosting a webpage like

<html>
....
    <script type="text/javascript" src="http://localhost:82/socket.io/socket.io.js"></script>
....
</html>

This is a necessary resource required for the socket.io client. So the resource loads on my server/dev pc, but not on my client pc. So I try:

<html>
....
    <script type="text/javascript" src="http://192.168.1.2:82/so开发者_开发知识库cket.io/socket.io.js"></script>
....
</html>

Now it doesn't work on either pc. I know that it should be

<script src="http://<uri:port>/socket.io/socket.io.js"></script>

as it says on the socket.io github, but I want to only test on a local network.

I've also looked at

<script type="text/javascript" src="http://cdn.socket.io/stable/socket.io.js"></script>

but I'm using socket.io 0.8.4 so the above version doesn't work.

So how can I get the socket.io resource served to the client in a local network environment? Or do you guys know of a website serving the 0.8.4 version of socket.io I could use?

Note: There are not firewall problems.


Try letting socket.io connect automagically with

var socket = io.connect();

That worked for me.


If you are using a different port of the same host to serve socket.io you can try using

<script>document.write('<script src="//'+ location.hostname + ':9998/socket.io/socket.io.js">\x3C/script>')</script>

which looks like a hack but it works, just replace 9998 with the port you're serving socket.io with.


Replace

<script type="text/javascript" src="http://localhost:82/socket.io/socket.io.js"></script>

with

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


for the index:

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

And from client connection:

var socket = io.connect('http://192.168.1.3:82', {'forceNew': true});

this worked for me!.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号