当前位置: 代码迷 >> 综合 >> swoole websocket 入门
  详细解决方案

swoole websocket 入门

热度:35   发布时间:2023-10-10 22:32:12.0

js发起websocket 

<!DOCTYPE html>
<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><title>1</title></head><body><script type="text/javascript" charset="utf-8">var wsServer = 'ws://127.0.0.1:2544';var webSocket = new WebSocket(wsServer);webSocket.onopen = function(evt){console.log('连接成功');}webSocket.onclose = function(evt){console.log('关闭');}webSocket.onmessage = function(evt){console.log(evt.data);}webSocket.onerror = function(evt,e){console.log('error');}</script></body>
</html>

php

<?php$server = new swoole_websocket_server('0.0.0.0',2544);//建立连接
$server->on('open',function($ws,$request){var_dump($request);$ws->push($request->fd,'welcome \n');
});// 接收信息
$server->on('message',function ($ws,$request){echo 'message:'.$request->data;$ws->push($request->fd,'get jt message');
});// 关闭连接
$server->on('close',function ($ws,$request){echo 'colse \n';
});$server->start();

 

  相关解决方案