简体   繁体   English

使用nodejs安装websocket

[英]installing websocket with nodejs

I am trying to install websocket on nodejs with npm I am getting following error.. I am installing it on windows. 我正在尝试使用npm在nodejs上安装websocket,但出现以下错误。我正在Windows上安装它。

npm install websocket

0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files (x86)\\nodejs\\\\node.exe',
1 verbose cli   'C:\\Program Files (x86)\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   'install',
1 verbose cli   'websocket@1.0.3' ]
2 info using npm@1.1.32
3 info using node@v0.8.0
4 verbose node symlink C:\Program Files (x86)\nodejs\\node.exe
5 verbose config file C:\Users\tasleem\.npmrc
6 verbose config file C:\Program Files (x86)\nodejs\etc\npmrc
7 verbose config file C:\Program Files (x86)\nodejs\node_modules\npm\npmrc
8 verbose read json C:\Program Files (x86)\nodejs\package.json
9 verbose read json C:\Program Files (x86)\nodejs\node_modules\npm\package.json
10 verbose read json C:\Program Files (x86)\nodejs\node_modules\package\package.json
11 verbose read json C:\Program Files (x86)\nodejs\node_modules\websocket-1.0.6\package.json
12 verbose read json C:\Program Files (x86)\nodejs\package.json
13 verbose cache add [ 'websocket@1.0.3', null ]
14 silly cache add name=undefined spec="websocket@1.0.3" args=["websocket@1.0.3",null]
15 verbose parsed url { pathname: 'websocket@1.0.3',
15 verbose parsed url   path: 'websocket@1.0.3',
15 verbose parsed url   href: 'websocket@1.0.3' }
16 silly cache add name="websocket" spec="1.0.3" args=["websocket","1.0.3"]
17 verbose parsed url { pathname: '1.0.3', path: '1.0.3', href: '1.0.3' }
18 verbose addNamed [ 'websocket', '1.0.3' ]
19 verbose addNamed [ '1.0.3', '1.0.3' ]
20 verbose url raw websocket/1.0.3
21 verbose url resolving [ 'https://registry.npmjs.org/', './websocket/1.0.3' ]
22 verbose url resolved https://registry.npmjs.org/websocket/1.0.3
23 http GET https://registry.npmjs.org/websocket/1.0.3
24 error Error: connect ETIMEDOUT
24 error     at errnoException (net.js:781:11)
24 error     at Object.afterConnect [as oncomplete] (net.js:772:19)
24 error  { [Error: connect ETIMEDOUT] code: 'ETIMEDOUT', errno: 'ETIMEDOUT', syscall: 'connect' }
25 error You may report this log at:
25 error     <http://github.com/isaacs/npm/issues>
25 error or email it to:
25 error     <npm-@googlegroups.com>
26 error System Windows_NT 6.1.7601
27 error command "C:\\Program Files (x86)\\nodejs\\\\node.exe" "C:\\Program Files (x86)\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "websocket@1.0.3"
28 error cwd C:\Program Files (x86)\nodejs
29 error node -v v0.8.0
30 error npm -v 1.1.32
31 error syscall connect
32 error code ETIMEDOUT
33 error message connect ETIMEDOUT
34 error errno ETIMEDOUT
35 error ETIMEDOUT errno
36 verbose exit [ 1, true ]

Or I have got the WebSocket package , could someone tell where to extract this inside nodejs folder? 或者我有WebSocket软件包,有人可以告诉我在nodejs文件夹中将其提取到哪里吗?

It seems that there is a connection problem between your site and the NPM registry . 您的站点和NPM注册表之间似乎存在连接问题。 Try first to connect to NPM registry in a browser, you should have the same error. 首先尝试在浏览器中连接到NPM注册表 ,您应该有同样的错误。

Check your network connectivity, you can also try to ping registry.npmjs.org . 检查您的网络连接性,您也可以尝试ping registry.npmjs.org

“ npm up -g”也不会伤害。

I am not sure if this can help you. 我不确定这是否可以帮助您。 I was using nodejs on windows via. 我在Windows上通过使用nodejs。 Cygwin and when I tried making javascripts with websockets I had problems with the standard node installation - it was too old, I then tried to install a new version of node from inside Cygwin, however this was NOT possible, so I proceeded to install it using "node-v0.10.24-x64.msi" which can be downloaded from http://nodejs.org/download/ Cygwin,当我尝试使用websockets制作javascript时,我遇到了标准节点安装的问题-它太旧了,然后我尝试从Cygwin内部安装新版本的节点,但是这是不可能的,所以我继续使用可以从http://nodejs.org/download/下载的“ node-v0.10.24-x64.msi”

After download I then copied manually all contents from folder nodejs to cygwin /usr/local/bin 下载后,我然后将所有内容手动从文件夹nodejs复制到cygwin / usr / local / bin

In order to install ws (websockets) then npm is needed and for that you need the newest version - after copying manually whole folder nodejs, then you can start cygwin and write following: 为了安装ws(websockets),则需要npm,为此,您需要最新版本-手动复制整个文件夹nodejs之后,可以启动cygwin并编写以下内容:

node -v

it should write something like this : 它应该写这样的东西:

$ node -v $节点-v

v0.10.24 v0.10.24

now you can procede with following: 现在您可以继续执行以下操作:

$ cd /usr/local/bin/node_modules/npm/bin

$ node npm-cli.js install ws

$ node npm-cli.js install websockets

the npm-cli.js is a command line interface version of npm, made in javascript and nodejs can parse it, so now ws module should be installed npm-cli.js是npm的命令行界面版本,使用javascript和nodejs可以解析,因此现在应该安装ws模块

-- NOTE it is very important that you copy or move the folder "node_modules" after the installation to following place : -注意,在安装后将文件夹“ node_modules”复制或移动到以下位置非常重要:

cygwin/home/ --your user-- /node_modules cygwin / home /-您的用户-/ node_modules

Otherwise your javascript projects will not beable to see the installed modules - it is actually strange why, but I have no answer for that - I just made a copy and then it worked 否则,您的javascript项目将无法查看已安装的模块-原因实际上很奇怪,但是对此我没有任何答案-我只是制作了一个副本,然后就可以了

I hope you can use this answer 我希望你可以用这个答案

Step1 : Set up server Express takes the role of HTTP server, serving HTML file and wiring Websocket service 步骤1:设置服务器 Express充当HTTP服务器的角色,提供HTML文件并连接Websocket服务

var express = require('express')
var ws = require('./ws')
var app = express()
app.get('/', function (req, res) {
   res.sendfile(__dirname + '/ws.html');
})
app.listen(3000, function () {
   console.log('app listening on port 3000!')
})

Step2 : Set up Client websocket client is a browser supported object. 步骤2:设置客户端 websocket客户端是浏览器支持的对象。

Below is goind to introduce 3 importand fucntion: 以下是介绍三个导入和功能的信息:

ws.onopen : emmited when connected ws.send : sending a send event to websocket server ws.onmessage : event emmited when receiving message ws.onopen:连接时发出ws.send:将发送事件发送到websocket服务器ws.onmessage:接收消息时发出的事件

    <script>
    var ws = new WebSocket('ws://localhost:40510');
    // event emmited when connected
    ws.onopen = function () {
        console.log('websocket is connected ...')
        // sending a send event to websocket server
        ws.send('connected')
    }
    // event emmited when receiving message 
    ws.onmessage = function (ev) {
        console.log(ev);
    }
</script>

For ur reference : https://hackernoon.com/nodejs-web-socket-example-tutorial-send-message-connect-express-set-up-easy-step-30347a2c5535 供您参考: https : //hackernoon.com/nodejs-web-socket-example-tutorial-send-message-connect-express-set-up-easy-step-30347a2c5535

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM