1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SocketCluster\Laravel; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\ServiceProvider as BaseServiceProvider; |
6
|
|
|
use Illuminate\Broadcasting\BroadcastManager; |
7
|
|
|
use WebSocket\Client as WebSocketClient; |
8
|
|
|
use SocketCluster\SocketCluster; |
9
|
|
|
|
10
|
|
|
class SCServiceProvider extends BaseServiceProvider |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Indicates if loading of the provider is deferred. |
14
|
|
|
* |
15
|
|
|
* @var bool |
16
|
|
|
*/ |
17
|
|
|
protected $defer = false; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Register bindings in the container. |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
public function register() |
25
|
|
|
{ |
26
|
|
|
$this->registerSocketCluster(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Register SocketCluster |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
protected function registerSocketCluster() |
35
|
|
|
{ |
36
|
|
|
$this->app->singleton('SocketCluster', function ($app) { |
37
|
|
|
$config = $app['config']['broadcasting']['connections']['socketcluster']; |
38
|
|
|
|
39
|
|
|
if (empty($config['uri'])) { |
40
|
|
|
$scheme = $config['secure']==true ? 'wss' : 'ws'; |
41
|
|
|
$host = trim($config['host'], "/"); |
42
|
|
|
$port = !empty($config['port']) ? ":".$config['port'] : ''; |
43
|
|
|
$path = trim($config['path'], "/"); |
44
|
|
|
$path = !empty($path) ? $path . "/" : ''; |
45
|
|
|
$config['uri'] = sprintf("%s://%s%s/%s", $scheme, $host, $port, $path); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$websocket = new WebSocketClient($config['uri']); |
49
|
|
|
return new SocketCluster($websocket); |
50
|
|
|
}); |
51
|
|
|
|
52
|
|
|
$this->app->alias('SocketCluster', SocketCluster::class); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Register new BroadcastManager in boot |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function boot() |
61
|
|
|
{ |
62
|
|
|
$this->app |
63
|
|
|
->make(BroadcastManager::class) |
64
|
|
|
->extend('socketcluster', function ($app) { |
65
|
|
|
return new SCBroadcaster($app['SocketCluster']); |
66
|
|
|
}); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the services provided by the provider. |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
public function provides() |
75
|
|
|
{ |
76
|
|
|
return ['SocketCluster']; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|