admin管理员组

文章数量:1289412

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/socket.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=".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.

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/socket.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.

Share Improve this question edited Sep 19, 2011 at 21:42 Derek asked Sep 19, 2011 at 1:44 DerekDerek 12.4k31 gold badges106 silver badges166 bronze badges 5
  • socket.io.js will only be server from the same machine your app is running, doesn't matter from what machine you are accessing it with. – Marcel M. Commented Sep 20, 2011 at 6:03
  • @Marcel M. No wat r u talking about? They are used on online chat apps all the time – Derek Commented Sep 21, 2011 at 0:51
  • I meant using localhost will only work when server and client is the same machine, no matter the port – Marcel M. Commented Sep 21, 2011 at 1:53
  • @MarcelM. I realize that, that's why it doesn't work, which is why I'm asking people for a way to run socket.io on a local network – Derek Commented Sep 21, 2011 at 20:31
  • What error message are you getting? Does "/socket.io/socket.io.js" exist from your web root dir on the server? – EhevuTov Commented Sep 21, 2011 at 22:36
Add a ment  | 

4 Answers 4

Reset to default 3

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!.

本文标签: javascriptSocketionodejs on local networkStack Overflow