如何使用rachet从socket.io.client发送消息到symfony2应用程序

时间:2015-05-22 07:30:05

标签: node.js sockets socket.io

Heey all,

我有一个带有Rachet的symfony2应用程序,效果非常好。当我使用浏览器访问它时,我可以看到日志记录,因此该部分似乎有效。

我也有一个nodejs应用程序,现在我想从该应用程序向symfony发送一条消息。那我做了什么:

我的想法是使用socket.io-client并执行以下操作:

var client = require("socket.io-client");
socket = client("tcp://domainname.dev:3344");
socket.emit('hello world');

但这似乎不起作用。我做错了什么?

已提前感谢

1 个答案:

答案 0 :(得分:0)

好吧,我经历了很多试验和错误会议,但我已经开始工作了。 在我说出答案之前,我会告诉你什么不该做。

Socket.io的客户端

我从socket.io-client开始。感谢Twitter上的某些人,我发现socketio客户端永远无法工作,因为客户端具有进行连接的特定握手。 (我找不到我的来源)

AutobahnJS

下一个选项:AutobahnJS,我在Ratchet的网站上看到它通过了高速公路测试套件http://socketo.me/docs/wamp。花生我想,但我没有得到它的工作,因为当前版本的AutobahnJS不支持WAMP的“旧”版本(https://groups.google.com/forum/#!topic/ratchet-php/Szd2VV4EtXs),但AutobahnJS 0.8。*应该。遗憾的是,您无法使用NPM安装该版本。

vagrant@dev:/var/www/quadstation.dev/quadstation-frontend$ sudo npm install autobahn@0.8.*
npm WARN package.json groundstation-quadcopter@0.0.1 No repository field.
npm ERR! Linux 3.13.0-32-generic
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "autobahn@0.8.*"
npm ERR! node v0.12.0
npm ERR! npm  v2.5.1
npm ERR! code ETARGET

npm ERR! notarget No compatible version found: autobahn@'>=0.8.0 <0.9.0'
npm ERR! notarget Valid install targets:
npm ERR! notarget ["0.0.0","0.9.0","0.9.0-2","0.9.0-3","0.9.0-4","0.9.0-5","0.9.1","0.9.1-3","0.9.1-4","0.9.2","0.9.3","0.9.4","0.9.4-2","0.9.5","0.9.6"]
npm ERR! notarget 
npm ERR! notarget This is most likely not a problem with npm itself.
npm ERR! notarget In most cases you or one of your dependencies are requesting
npm ERR! notarget a package version that doesn't exist.

npm ERR! Please include the following file with any support request:
npm ERR!     /var/www/quadstation.dev/quadstation-frontend/npm-debug.log

vagrant@dev:/var/www/quadstation.dev/quadstation-frontend$ npm install https://github.com/tavendo/AutobahnJS/archive/v0.8.2.tar.gz
npm WARN package.json groundstation-quadcopter@0.0.1 No repository field.
npm ERR! Linux 3.13.0-32-generic
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "https://github.com/tavendo/AutobahnJS/archive/v0.8.2.tar.gz"
npm ERR! node v0.12.0
npm ERR! npm  v2.5.1
npm ERR! path /tmp/npm-30480-482d323b/unpack-e44042007b9f/package.json
npm ERR! code ENOENT
npm ERR! errno -2

npm ERR! enoent ENOENT, open '/tmp/npm-30480-482d323b/unpack-e44042007b9f/package.json'
npm ERR! enoent This is most likely not a problem with npm itself
npm ERR! enoent and is related to npm not being able to find a file.
npm ERR! enoent 

npm ERR! Please include the following file with any support request:
npm ERR!     /var/www/quadstation.dev/quadstation-frontend/npm-debug.log

WebSocket的节点

在那之后我变得绝望并且尝试了Websockets-Node但是我没有进一步,然后连接被拒绝但我没有得到任何明确的理由为什么或如何解决它。

那么什么是正确的解决方案呢? WS和大量阅读

我之前尝试过ws,但经过一些额外的阅读后,我给了它第二次机会,直到我改变了一些东西,我才开始工作。我的初始代码如下:

var WebSocket = require('ws');
var ws = new WebSocket('ws://localhost:3344/', {
    protocolVersion: 8,
    origin: 'http://localhost'
});

在阅读了一些协议之后,我发现protocolVersion 13与rfc6455 + What are the protocol differences between WebSockets versions?

相关

我还发现(感谢这个帖子:https://code.google.com/p/chromium/issues/detail?id=36652)使用localhost也让我感到头疼。我把它改为127.0.0.1,最后我得到了工作情况。

var WebSocket = require('ws');
var ws = new WebSocket('ws://127.0.0.1:3344/', {
    protocolVersion: 13,
    origin: 'http://127.0.0.1'
});

如果有人对此有其他疑问,请告诉我。