Skip to content

Commit fcc2606

Browse files
author
谢彪
committed
support custom event
1 parent 5aee564 commit fcc2606

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
PHP SSE: Server-sent Events
22
======
33

4-
A simple and efficient library implemented HTML5's server-sent events by PHP, is used to real-time push events from server to client, and easier than Websocket, instead of AJAX request.
4+
A simple and efficient library implemented HTML5's server-sent events by PHP, is used to real-time push events from server to client, and easier than
5+
Websocket, instead of AJAX request.
56

67
## Requirements
78

@@ -14,9 +15,11 @@ composer require "hhxsv5/php-sse:~2.0" -vvv
1415
```
1516

1617
## Usage
18+
1719
### Run demo
1820

1921
- Run PHP webserver
22+
2023
```Bash
2124
cd examples
2225
php -S 127.0.0.1:9001 -t .
@@ -27,18 +30,20 @@ php -S 127.0.0.1:9001 -t .
2730
![Demo](https://raw.githubusercontent.com/hhxsv5/php-sse/master/sse.png)
2831

2932
### Javascript demo
33+
3034
> Client: receiving events from the server.
3135
3236
```Javascript
3337
// withCredentials=true: pass the cross-domain cookies to server-side
34-
const source = new EventSource('http://127.0.0.1:9001/sse.php', {withCredentials:true});
35-
source.addEventListener('news', function(event) {
38+
const source = new EventSource('http://127.0.0.1:9001/sse.php', {withCredentials: true});
39+
source.addEventListener('news', function (event) {
3640
console.log(event.data);
3741
// source.close(); // disconnect stream
3842
}, false);
3943
```
4044

4145
### PHP demo
46+
4247
> Server: Sending events by pure php.
4348
4449
```PHP
@@ -64,12 +69,14 @@ $callback = function () {
6469
throw new StopSSEException();
6570
}
6671
return json_encode(compact('news'));
72+
// return ['event' => 'ping', 'data' => 'ping data']; // Custom event temporarily: send ping event
6773
// return ['id' => uniqid(), 'data' => json_encode(compact('news'))]; // Custom event Id
6874
};
6975
(new SSE(new Event($callback, 'news')))->start(3);
7076
```
7177

7278
### Symfony and Laravel demo
79+
7380
> Server: Sending events by Laravel or Symfony.
7481
7582
```PHP
@@ -97,6 +104,7 @@ public function getNewsStream()
97104
throw new StopSSEException();
98105
}
99106
return json_encode(compact('news'));
107+
// return ['event' => 'ping', 'data' => 'ping data']; // Custom event temporarily: send ping event
100108
// return ['id' => uniqid(), 'data' => json_encode(compact('news'))]; // Custom event Id
101109
};
102110
(new SSE(new Event($callback, 'news')))->start(3);
@@ -106,6 +114,7 @@ public function getNewsStream()
106114
```
107115

108116
### Swoole demo
117+
109118
> Server: Sending events by Swoole Coroutine Http Server.
110119
> Install [Swoole](https://github.com/swoole/swoole-src) 4.5.x: `pecl install swoole`.
111120
@@ -149,9 +158,10 @@ $server->on('Request', function (Request $request, Response $response) use ($ser
149158
throw new StopSSEException();
150159
}
151160
return json_encode(compact('news'));
161+
// return ['event' => 'ping', 'data' => 'ping data']; // Custom event temporarily: send ping event
152162
// return ['id' => uniqid(), 'data' => json_encode(compact('news'))]; // Custom event Id
153163
}, 'news');
154-
(new SSESwoole($event, $request, $response))->start(3);
164+
(new SSESwoole($event, $request, $response))->start();
155165
});
156166
$server->start();
157167
```

examples/sse-swoole.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
include '../vendor/autoload.php';
34

45
use Hhxsv5\SSE\Event;
@@ -40,6 +41,7 @@
4041
throw new StopSSEException();
4142
}
4243
return json_encode(compact('news'));
44+
// return ['event' => 'ping', 'data' => 'ping data']; // Custom event temporarily: send ping event
4345
// return ['id' => uniqid(), 'data' => json_encode(compact('news'))]; // Custom event Id
4446
}, 'news');
4547
(new SSESwoole($event, $request, $response))->start(3);

examples/sse.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
include '../vendor/autoload.php';
34

45
use Hhxsv5\SSE\Event;
@@ -23,6 +24,7 @@
2324
throw new StopSSEException();
2425
}
2526
return json_encode(compact('news'));
27+
// return ['event' => 'ping', 'data' => 'ping data']; // Custom event temporarily: send ping event
2628
// return ['id' => uniqid(), 'data' => json_encode(compact('news'))]; // Custom event Id
2729
};
2830
(new SSE(new Event($callback, 'news')))->start(3);

src/Event.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ class Event
1414
*/
1515
protected $event;
1616

17+
/**
18+
* @var string The initial $event
19+
*/
20+
protected $initialEvent;
21+
1722
/**
1823
* @var string The data field for the message. When the EventSource receives multiple consecutive lines that begin with data:, it will concatenate them, inserting a newline character between each one. Trailing newlines are removed.
1924
*/
@@ -37,15 +42,15 @@ class Event
3742
/**
3843
* Event constructor.
3944
* @param callable $callback {@see Event::$callback}
40-
* @param string $event {@see Event::$event}
41-
* @param int $retry {@see Event::$retry}
45+
* @param string $event {@see Event::$event}
46+
* @param int $retry {@see Event::$retry}
4247
*/
4348
public function __construct(callable $callback, $event = '', $retry = 5000)
4449
{
4550
$this->callback = $callback;
4651
$this->id = '';
4752
$this->data = '';
48-
$this->event = $event;
53+
$this->initialEvent = $this->event = $event;
4954
$this->retry = $retry;
5055
$this->comment = '';
5156
}
@@ -57,15 +62,19 @@ public function __construct(callable $callback, $event = '', $retry = 5000)
5762
*/
5863
public function fill()
5964
{
65+
$this->event = $this->initialEvent;
6066
$result = call_user_func($this->callback);
6167
if ($result === false) {
6268
$this->id = '';
6369
$this->data = '';
6470
$this->comment = 'no data';
6571
} else {
72+
if (isset($result['event'])) {
73+
$this->event = $result['event'];
74+
}
6675
$this->id = isset($result['id']) ? $result['id'] : str_replace('.', '', uniqid('', true));
6776
$this->data = isset($result['data']) ? $result['data'] : $result;
68-
$this->comment = '';
77+
$this->comment = isset($result['comment']) ? $result['comment'] : '';
6978
}
7079
return $this;
7180
}

0 commit comments

Comments
 (0)