Laravel使用swoole实现websocket主动消息推送的方法介绍

2025-05-29 0 28

需求

需要实现一个可以主动触发消息推送的功能,这个可以实现向模板消息那个,给予所有成员发送自定义消息,而不需要通过客户端发送消息,服务端上message中监听传送的消息进行做相对于的业务逻辑。

主动消息推送实现

平常我们采用 swoole 来写 WebSocket 服务可能最多的用到的是open,message,close这三个监听状态,但是万万没有看下下面的onRequest回调的使用,没错,解决这次主动消息推送的就是需要用onRequest回调。

官方文档:正因为swoole_websocket_server继承自swoole_http_server,所以在 websocket 中有onRequest回调。

详细实现

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83
# 这里是一个laravel中Commands

# 运行php artisan swoole start 即可运行

<?php

namespace App\\Console\\Commands;

use Illuminate\\Console\\Command;

use swoole_websocket_server;

class Swoole extends Command

{

public $ws;

/**

* The name and signature of the console command.

*

* @var string

*/

protected $signature = 'swoole {action}';

/**

* The console command description.

*

* @var string

*/

protected $description = 'Active Push Message';

/**

* Create a new command instance.

*

* @return void

*/

public function __construct()

{

parent::__construct();

}

/**

* Execute the console command.

*

* @return mixed

*/

public function handle()

{

$arg = $this->argument('action');

switch ($arg) {

case 'start':

$this->info('swoole server started');

$this->start();

break;

case 'stop':

$this->info('swoole server stoped');

break;

case 'restart':

$this->info('swoole server restarted');

break;

}

}

/**

* 启动Swoole

*/

private function start()

{

$this->ws = new swoole_websocket_server("0.0.0.0", 9502);

//监听WebSocket连接打开事件

$this->ws->on('open', function ($ws, $request) {

});

//监听WebSocket消息事件

$this->ws->on('message', function ($ws, $frame) {

$this->info("client is SendMessage\\n");

});

//监听WebSocket主动推送消息事件

$this->ws->on('request', function ($request, $response) {

$scene = $request->post['scene']; // 获取值

$this->info("client is PushMessage\\n".$scene);

});

//监听WebSocket连接关闭事件

$this->ws->on('close', function ($ws, $fd) {

$this->info("client is close\\n");

});

$this->ws->start();

}

}

前面说的是 swoole 中onRequest的实现,下面实现下在控制器中主动触发onRequest回调。实现方法就是我们熟悉的curl请求。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25
# 调用activepush方法以后,会在cmd中打印出

# client is PushMessage 主动推送消息 字眼

/**

* CURL请求

* @param $data

*/

public function curl($data)

{

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, "http://127.0.0.1:9502");

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($curl, CURLOPT_HEADER, 1);

curl_setopt($curl, CURLOPT_POST, 1);

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

curl_exec($curl);

curl_close($curl);

}

/**

* 主动触发

*/

public function activepush()

{

$param['scene'] = '主动推送消息';

$this->curl($param); // 主动推送消息

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对快网idc的支持。

原文链接:https://segmentfault.com/a/1190000020731965

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Laravel使用swoole实现websocket主动消息推送的方法介绍 https://www.kuaiidc.com/91293.html

相关文章

发表评论
暂无评论