node.js socket.io 客户端库在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8528681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where is the socket.io client library?
提问by Braun Shedd
As far as I have seen, there is no explanation as to where we are to locate the client side script for socket.ioif node.jsis 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?
就我所见,没有解释我们将在哪里定位客户端脚本,因为socket.ioifnode.js没有用作 Web 服务器。我找到了一个完整的客户端文件目录,但我需要它们的组合版本(就像使用 node.js webs 服务器时提供的一样)。有任何想法吗?
采纳答案by EhevuTov
socket.io.js is what you're going to put into your client-side html. Something like:
socket.io.js 是您要放入客户端 html 的内容。就像是:
<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.
将该文件复制到您希望服务器为其提供服务的位置。
回答by Matt Way
The best way I have found to do this is to use bower.
我发现这样做的最好方法是使用bower。
bower install socket.io-client --save
and include the following in your app's HTML:
并在您的应用的 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.
这样,您就可以像对待任何其他受管软件包一样对待客户端的 socket.io 部分。
回答by Capaj
I think that better and proper way is to load it from this url
我认为更好和正确的方法是从这个 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.
在 socket.io 运行的域上。此解决方案的积极之处在于,如果您更新 socket.io npm 模块,您的客户端文件也会更新,您不必每次都手动复制它。
回答by Dave Ceddia
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.jsonfile.
我按照 Matt Way 的回答中的建议使用了 bower,效果很好,但是库本身没有自己的bower.json文件。
This meant that the bower-main-filesGulp 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.jsonworked around the issue.
这意味着bower-main-files我用来查找依赖项的 JS 文件的Gulp 插件没有拉入 socket.io,并且我在页面加载时遇到错误。为我的项目添加覆盖bower.json解决了这个问题。
First install the library with bower:
首先使用 bower 安装库:
bower install socket.io-client --save
Then add the override to your project's bower.json:
然后将覆盖添加到项目的 bower.json 中:
"overrides": {
"socket.io-client": {
"main": ["socket.io.js"]
}
}
回答by K. D.
For everyone who runs wiredep and gets the "socket.io-client was not injected in your file." error:
对于运行wiredep并获得“socket.io-client未注入您的文件”的每个人。错误:
Modify your wiredep task like this:
像这样修改您的有线任务:
wiredep: {
..
main: {
..
overrides: {
'socket.io-client': {
main: 'socket.io.js'
}
}
}
回答by Sina Sharafzadeh
回答by Bienvenido David
If you are using bower.json, add the socket.io-client dependency.
如果您使用 bower.json,请添加 socket.io-client 依赖项。
"socket.io-client": "0.9.x"
Then run bower installto download socket.io-client.
然后运行bower install来下载 socket.io-client。
Then add the script tag in your HTML.
然后在您的 HTML 中添加脚本标记。
<script src="bower_components/socket.io-client/dist/socket.io.min.js"></script>
回答by IHeartAndroid
if you use https://github.com/btford/angular-socket-iomake sure to have your index.html like this:
如果您使用https://github.com/btford/angular-socket-io,请确保您的 index.html 如下所示:
<!-- 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>

