我在Debian v8.2
上运行rabbitmq-server v3.3.5-1.1
。我已经根据文档中的建议启用了rabbitmq_web_stomp
和rabbitmq_web_stomp_examples
:
rabbitmq-plugins enable rabbitmq_web_stomp
rabbitmq-plugins enable rabbitmq_web_stomp_examples
http://127.0.0.1:15670
公开的所有示例都按预期工作,但它们都使用SockJS
而不是本机浏览器的WebSocket
:
// Stomp.js boilerplate
var ws = new SockJS('http://' + window.location.hostname + ':15674/stomp');
var client = Stomp.over(ws);
我想坚持WebSocket
,所以我尝试了文档中的建议:
var ws = new WebSocket('ws://127.0.0.1:15674/ws');
这给我带来了一个错误:
WebSocket connection to 'ws://127.0.0.1:15674/ws' failed: Error during WebSocket handshake: Unexpected response code: 404
用netcat
进一步测试可确认404
:
# netcat -nv 127.0.0.1 15674
127.0.0.1 15674 open
GET /ws HTTP/1.1
Host: 127.0.0.1
HTTP/1.1 404 Not Found
Connection: close
Content-Length: 0
Date: Sat, 23 Jan 2016 20:15:13 GMT
Server: Cowboy
显然cowboy
不会暴露/ws
路径,所以我想知道:
- 在这种情况下是否可以重新配置
cowboy
?如何?值得吗? - 我可以使用
nginx
代替cowboy
(首选选项)吗?如何? - 我还有哪些其他选择?
编辑
RabbitMQ 文档具有误导性。正确的 WebSocket URI:
http://127.0.0.1:15674/stomp/websocket
干得好,但是:
new WebSocket('http://127.0.0.1:15674/stomp/websocket')
VM98:2 Uncaught DOMException: Failed to construct 'WebSocket': The URL's scheme must be either 'ws' or 'wss'. 'http' is not allowed.(…)(anonymous function) ...
需要使用 WS/WSS 架构:
new WebSocket('ws://127.0.0.1:15674/stomp/websocket')
WebSocket {url: "ws://127.0.0.1:15674/stomp/websocket", readyState: 0, bufferedAmount: 0, onopen: null, onerror: null…}