Where is the socket.io client library?

2019-02-01 20:13发布

As far as I have seen, there is no explanation as to where we are to locate the client side script for socket.io if node.js is not used as the web server. I've found a whole directory of client side files, but I need them in a combined version (like it's served when using node.js webs servers). Any ideas?

8条回答
唯我独甜
2楼-- · 2019-02-01 20:31

socket.io.js is what you're going to put into your client-side html. Something like:

<script type="text/javascript" src="socket.io.js"></script>

my script is located:

/usr/local/lib/node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js

copy that file to where you want your server to serve it.

查看更多
beautiful°
3楼-- · 2019-02-01 20:31

The best way I have found to do this is to use bower.

bower install socket.io-client --save

and include the following in your app's HTML:

<script src="/bower_components/socket.io-client/socket.io.js"></script>

That way you can treat the socket.io part of your client the same way you treat any other managed package.

查看更多
混吃等死
4楼-- · 2019-02-01 20:32

if you use https://github.com/btford/angular-socket-io make sure to have your index.html like this:

<!-- https://raw.githubusercontent.com/socketio/socket.io-client/master/socket.io.js -->
<script src="socket.io.js"></script>

<!-- build:js({client,node_modules}) app/vendor.js -->
<!-- bower:js -->
<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="bower_components/angular/angular.js"></script>
<!-- ...... -->
<script src="bower_components/angular-socket-io/socket.js"></script>
<!-- endbower -->
<!-- endbuild -->



<script type="text/javascript" charset="utf-8">
   angular.module('myapp', [
// ...    
'btford.socket-io'
]);

// do your angular/socket stuff
</script>
查看更多
太酷不给撩
5楼-- · 2019-02-01 20:35

If you are using bower.json, add the socket.io-client dependency.

"socket.io-client": "0.9.x"

Then run bower install to download socket.io-client.

Then add the script tag in your HTML.

<script src="bower_components/socket.io-client/dist/socket.io.min.js"></script>
查看更多
ゆ 、 Hurt°
6楼-- · 2019-02-01 20:38

For everyone who runs wiredep and gets the "socket.io-client was not injected in your file." error:

Modify your wiredep task like this:

wiredep: {
  ..
  main: {
    ..
    overrides: {
      'socket.io-client': {
        main: 'socket.io.js'
      }
    }
  }
查看更多
淡お忘
7楼-- · 2019-02-01 20:42

I think that better and proper way is to load it from this url

src="/socket.io/socket.io.js" 

on the domain where socket.io runs. What is positive on this solution is that if you update your socket.io npm module, your client file gets updated too and you don't have to copy it every time manually.

查看更多
登录 后发表回答