Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websocket sample #26

Closed
ljfreelancer88 opened this issue May 10, 2021 · 4 comments
Closed

Websocket sample #26

ljfreelancer88 opened this issue May 10, 2021 · 4 comments

Comments

@ljfreelancer88
Copy link

Hi, how can I make websocket working on Nano? An example please and thank you.

@huangdijia
Copy link
Member

$app = AppFactory::createBase('0.0.0.0', 9501);
$app->config(['server.servers.1' => [
        'name' => 'ws',
        'type' => Server::SERVER_WEBSOCKET,
        'host' => '0.0.0.0',
        'port' => 9502,
        'sock_type' => SWOOLE_SOCK_TCP,
        'callbacks' => [
            Event::ON_HAND_SHAKE => [\Hyperf\WebSocketServer\Server::class, 'onHandShake'],
            Event::ON_MESSAGE => [\Hyperf\WebSocketServer\Server::class, 'onMessage'],
            Event::ON_CLOSE => [\Hyperf\WebSocketServer\Server::class, 'onClose'],
        ],
    ],
]);
$app->run();

记得先安装 hyperf/websocket-server

@as168795
Copy link

as168795 commented Apr 1, 2023

你好

$app = AppFactory::createBase('0.0.0.0', 9501);
$app->config(['server.servers.1' => [
        'name' => 'ws',
        'type' => Server::SERVER_WEBSOCKET,
        'host' => '0.0.0.0',
        'port' => 9502,
        'sock_type' => SWOOLE_SOCK_TCP,
        'callbacks' => [
            Event::ON_HAND_SHAKE => [\Hyperf\WebSocketServer\Server::class, 'onHandShake'],
            Event::ON_MESSAGE => [\Hyperf\WebSocketServer\Server::class, 'onMessage'],
            Event::ON_CLOSE => [\Hyperf\WebSocketServer\Server::class, 'onClose'],
        ],
    ],
]);
$app->run();

记得先安装 hyperf/websocket-server

你好,是否能提供更详细的实例

use Hyperf\Nano\Factory\AppFactory;
use Hyperf\Server\Server;
use Hyperf\Server\Event;
require_once __DIR__ . '/vendor/autoload.php';

$app = AppFactory::createBase('0.0.0.0', 9503);
$app->config(['server.servers.1' => [
        'name' => 'ws',
        'type' => Server::SERVER_WEBSOCKET,
        'host' => '0.0.0.0',
        'port' => 9502,
        'sock_type' => SWOOLE_SOCK_TCP,
        'callbacks' => [
            Event::ON_HAND_SHAKE => [\Hyperf\WebSocketServer\Server::class, 'onHandShake'],
            Event::ON_MESSAGE => [\Hyperf\WebSocketServer\Server::class, 'onMessage'],
            Event::ON_CLOSE => [\Hyperf\WebSocketServer\Server::class, 'onClose'],
        ],
    ],
]);
$app->run();

我在使用上述代码时运行服务正常,但网页端建立链接时报错:

[DEBUG] WebSocket: fd[4] start a handshake request.
[WARNING] Hyperf\HttpMessage\Exception\NotFoundHttpException: Not Found(0) in /data/project/vendor/hyperf/http-server/src/CoreMiddleware.php:173

@huangdijia
Copy link
Member

参考:huangdijia/nano-demo@f0f143d

@as168795
Copy link

as168795 commented Apr 3, 2023

好的,非常感谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants