web套接字握手时出错;websocket连接失败,使用autobahn.js
我正在尝试使用Ratchet、React和Autobahn创建websocket服务器。尝试连接时,控制台中出现此错误 autobahn.min.js:196到“ws://localhost:8090/”的WebSocket连接 失败:WebSocket握手期间出错:意外响应代码: 426 在谷歌的一些挖掘过程中,我发现Ratchet只支持WAMP1,但那个信息是2014年的。这是否仍然相关 这是JSweb套接字握手时出错;websocket连接失败,使用autobahn.js,websocket,autobahn,ratchet,wamp-protocol,autobahnjs,Websocket,Autobahn,Ratchet,Wamp Protocol,Autobahnjs,我正在尝试使用Ratchet、React和Autobahn创建websocket服务器。尝试连接时,控制台中出现此错误 autobahn.min.js:196到“ws://localhost:8090/”的WebSocket连接 失败:WebSocket握手期间出错:意外响应代码: 426 在谷歌的一些挖掘过程中,我发现Ratchet只支持WAMP1,但那个信息是2014年的。这是否仍然相关 这是JS var connection = new autobahn.Connection({
var connection = new autobahn.Connection({
transports: [{
type: 'websocket',
port: 8090,
host: 'localhost',
url: 'ws://localhost:8090'
}],
realm: 'realm1'
});
connection.open();
这是WebsocketController类
<?php
namespace App\Http\Controllers;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\WampServerInterface;
class WebSocketController extends Controller implements WampServerInterface{
private $connections = [];
protected $subscribedTopics = array();
public function onSubscribe(ConnectionInterface $conn, $topic) {
$this->subscribedTopics[$topic->getId()] = $topic;
}
public function onUnSubscribe(ConnectionInterface $conn, $topic) {
}
public function onOpen(ConnectionInterface $conn) {
$this->connections->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
}
public function onClose(ConnectionInterface $conn) {
}
public function onCall(ConnectionInterface $conn, $id, $topic, array $params) {
// In this application if clients send data it's because the user hacked around in console
$conn->callError($id, $topic, 'You are not allowed to make calls')->close();
}
public function onPublish(ConnectionInterface $conn, $topic, $event, array $exclude, array $eligible) {
// In this application if clients send data it's because the user hacked around in console
$conn->close();
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "New error!".$e;
}
/**
* @param string JSON'ified string we'll receive from ZeroMQ
*/
public function onBlogEntry($entry) {
$entryData = json_decode($entry, true);
// If the lookup topic object isn't set there is no one to publish to
if (!array_key_exists($entryData['category'], $this->subscribedTopics)) {
return;
}
$topic = $this->subscribedTopics[$entryData['category']];
// re-send the data to all the clients subscribed to that category
$topic->broadcast($entryData);
}
}
标题
Request URL:ws://localhost:8090/
Request Method:GET
Status Code:426 No Sec-WebSocket-Protocols requested supported
Response Headers
view source
Connection:Upgrade
Sec-WebSocket-Protocol:0
Sec-WebSocket-Version:13
Transfer-Encoding:chunked
Upgrade:websocket
X-Powered-By:Ratchet/0.4
Request Headers
view source
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.9,et;q=0.8
Cache-Control:no-cache
Connection:Upgrade
Host:localhost:8090
Origin:http://ermp.ee:8000
Pragma:no-cache
Sec-WebSocket-Extensions:permessage-deflate; client_max_window_bits
Sec-WebSocket-Key:GbJ7qf3lzKDE2hmh3mxJpQ==
Sec-WebSocket-Protocol:wamp.2.json, wamp.2.msgpack
Sec-WebSocket-Version:13
Upgrade:websocket
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36
我做错了什么 遇到这个问题时,我正在制作类似于您的程序的东西。我意识到我在错误的端口上运行localhost,并且在所需的选定端口上后台运行了Skype之类的进程。 本地主机在端口8000上运行。 将上面的代码更改为此,并尝试在我的一个项目中使用此代码
var connection = new autobahn.Connection({
transports: [{
type: 'websocket',
port: 8000,
host: 'localhost',
url: 'ws://localhost:8000'
}],
realm: 'realm1'
}));
connection.open()
另外,请检查您的计算机上是否已经有一个应用程序在端口8000上运行,是否需要结束程序/进程以解决问题
在服务器中:
$webSock = new \React\Socket\Server('0.0.0.0:8000', $loop); // Binding to 0.0.0.0 means remotes can connect
在标题中:
Host:localhost:8000
您知道ratchet只支持wamp1
也许您可以将ratchet切换到Throuway,这可能会解决问题我将ratchet切换到ThrouwayPHP,如果其他人有相同的问题,它会立即开始工作 我不确定我是否正确,但本地主机不是在8080端口上运行,或者不是在您的计算机中指定的端口上运行。@Omkaar.K您可以让本地主机运行您想要的任何端口。我的运行速度是8000检查你的设置如果你的运行速度是8000我也有同样的问题,因为我从互联网上复制了代码。然后我注意到我的本地主机运行在其他端口8000@Omkaar上。KRatchet只支持wamp1,我知道,但Autobahn js最新版本支持wamp1或wamp2,但我不知道如何更改协议,我把赏金给了你谢谢@raqulka
Host:localhost:8000