javascript 如何命中 WebSocket 端点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24704097/
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
How to hit the WebSocket Endpoint?
提问by daydreamer
I see that there is websocket endpoint which works out fins with Java tests. In logs I see
我看到有 websocket 端点可以通过 Java 测试解决问题。在日志中我看到
Connecting to: ws://127.0.0.1:8080/76f48a44-0af8-444c-ba97-3f1ed34afc91/tweets
Just like any other REST
API I would like to hit it via browser or curl, but when I do that I see
就像任何其他REST
API 一样,我想通过浏览器或 curl 访问它,但是当我这样做时,我看到
? tweetstream git:(master) ? curl ws://127.0.0.1:8080/b9b90525-4cd4-43de-b893-7ef107ad06c2/tweets
curl: (1) Protocol ws not supported or disabled in libcurl
and
和
? tweetstream git:(master) ? curl http://127.0.0.1:8080/b9b90525-4cd4-43de-b893-7ef107ad06c2/tweets
<html><head><title>Error</title></head><body>Not Found</body></html>%
Is there a way to test websocket APIs with browser/curl?
有没有办法用浏览器/curl 测试 websocket API?
采纳答案by Lee Adams
If you mean literally to test the implementation of websockets, I found Autobahn's test suite to be very useful: http://autobahn.ws/
如果你真的想测试 websockets 的实现,我发现 Autobahn 的测试套件非常有用:http: //autobahn.ws/
If you just want to noodle with a websocket I would recommend using the developer tools in a browser like chrome to make a connection and send/recv data:
如果您只想使用 websocket,我建议您在 chrome 等浏览器中使用开发人员工具来建立连接并发送/接收数据:
var ws = new WebSocket("ws://127.0.0.1:8080/76f48a44-0af8-444c-ba97-3f1ed34afc91/tweets");
ws.onclose = function() { // thing to do on close
};
ws.onerror = function() { // thing to do on error
};
ws.onmessage = function() { // thing to do on message
};
ws.onopen = function() { // thing to do on open
};
ws.send("Hello World");
回答by Damian
This did the trick for me:
这对我有用:
$ curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Host: echo.websocket.org" -H "Origin: http://www.websocket.org" http://echo.websocket.org
from: http://www.thenerdary.net/post/24889968081/debugging-websockets-with-curl
来自:http: //www.thenerdary.net/post/24889968081/debugging-websockets-with-curl
回答by facundofarias
I had to use this command to make it work:
我必须使用此命令才能使其工作:
$ curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Host: echo.websocket.org" -H "Origin: http://www.websocket.org" -H "Sec-WebSocket-Version: 13" -H 'Sec-WebSocket-Key: +onQ3ZxjWlkNa0na6ydhNg==' http://www.websocket.org
I am using Jetty, and if I didn't add the Sec-WebSocket-Version/Sec-WebSocket-Key doesn't work. Just for the record.
我正在使用 Jetty,如果我没有添加 Sec-WebSocket-Version/Sec-WebSocket-Key 不起作用。只是为了记录。
回答by Vinod Kannan
回答by Vi.
回答by user3697288
I wrote a cURL like WebSocket client. You can send single data frame (text/binary) via this tool and then it closes the socket. Also you can add HTTP headers in the initial HTTP upgrade process.
我写了一个像 WebSocket 客户端一样的 cURL。您可以通过此工具发送单个数据帧(文本/二进制),然后关闭套接字。您也可以在初始 HTTP 升级过程中添加 HTTP 标头。