套接字.io 客户端库在哪里?

就我所见,如果 node.js不用作 Web 服务器,那么我们在哪里找到 socket.io的客户端脚本就没有解释。我已经找到了一个完整的客户端文件目录,但是我需要它们的组合版本(就像使用 node.js Web 服务器时提供的那样)。有什么想法吗?

39760 次浏览

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.

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.

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.

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>

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'
}
}
}

I have created a bower compatible socket.io-client that can be install like this :

bower install sio-client --save

or for development usage :

bower install sio-client --save-dev

link to repo

I used bower as suggested in Matt Way's answer, and that worked great, but then the library itself didn't have its own bower.json file.

This meant that the bower-main-files Gulp plugin that I'm using to find my dependencies' JS files did not pull in socket.io, and I was getting an error on page load. Adding an override to my project's bower.json worked around the issue.

First install the library with bower:

bower install socket.io-client --save

Then add the override to your project's bower.json:

"overrides": {
"socket.io-client": {
"main": ["socket.io.js"]
}
}

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>