├── .gitignore ├── CODE_OF_CONDUCT.md ├── LICENSE ├── README.md ├── assets └── Socket.js ├── composer.json ├── config └── socket.php └── src ├── Client.php ├── Clients.php ├── Commands └── Listen.php ├── Events ├── ClientConnected.php ├── ClientDisconnected.php ├── Event.php └── MessageReceived.php ├── Facades └── Socket.php ├── Message.php ├── MessageComponent.php ├── Server.php ├── SessionManager.php ├── Socket.php └── SocketServiceProvider.php /.gitignore: -------------------------------------------------------------------------------- 1 | # Someday 2 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our Standards 8 | 9 | Examples of behavior that contributes to creating a positive environment include: 10 | 11 | * Using welcoming and inclusive language 12 | * Being respectful of differing viewpoints and experiences 13 | * Gracefully accepting constructive criticism 14 | * Focusing on what is best for the community 15 | * Showing empathy towards other community members 16 | 17 | Examples of unacceptable behavior by participants include: 18 | 19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances 20 | * Trolling, insulting/derogatory comments, and personal or political attacks 21 | * Public or private harassment 22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | * Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our Responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Enforcement 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at info@codemash.be. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. 38 | 39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. 40 | 41 | ## Attribution 42 | 43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] 44 | 45 | [homepage]: http://contributor-covenant.org 46 | [version]: http://contributor-covenant.org/version/1/4/ 47 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Codemash 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Socket 2 | 3 | This package allows you to use sockets easily and elegantly in your Laravel 5 application. Based on the awesome PHP socket library, [Ratchet](https://github.com/ratchetphp/Ratchet). Read the instructions below to get setup. 4 | 5 | ## Requirements 6 | 7 | Laravel 5.x. 8 | 9 | ## Installation 10 | 11 | 12 | You can install the package using the [Composer](https://getcomposer.org/) package manager. You can install it by running this command in your project root: 13 | 14 | ```sh 15 | composer require codemash/laravel-socket 16 | ``` 17 | 18 | Add the `Codemash\Socket\SocketServiceProvider` provider to the `providers` array in `config/app.php`': 19 | 20 | ```php 21 | 'providers' => [ 22 | ... 23 | Codemash\Socket\SocketServiceProvider::class, 24 | ], 25 | ``` 26 | 27 | Then, add the facade to your `aliases` array. The default facade provides an easy-to-use interface to integrate the socket files in your view. 28 | 29 | ```php 30 | 'aliases' => [ 31 | ... 32 | 'Socket' => Codemash\Socket\Facades\Socket::class, 33 | ] 34 | ``` 35 | 36 | Finally, the config and the javascript files need to be published, which can be done by running the following command: 37 | 38 | ```sh 39 | php artisan vendor:publish --provider="Codemash\Socket\SocketServiceProvider" 40 | ``` 41 | 42 | The published assets can be found at `config/socket.php` and the default javascript at `public/vendor/socket/socket.js`. It is important to know that the `Socket::javascript()` facade function will include both a default socket located at `window.appSocket` and the `socket.js` source file located in the vendor folder. These are merely a start, and provide a quick way to work with the sockets but you are always free to write a custom implementation. 43 | 44 | ## Getting started 45 | 46 | Let's create a simple application that sends a message to all other connected clients. When a socket action occurs, it will be wrapped around a Laravel event and triggered. This is a great way for us to catch these events and act upon them. Let's register our listener in the `app/Providers/EventServiceProvider.php` file like this: 47 | 48 | ```php 49 | message; 87 | 88 | // If the incomming command is 'sendMessageToOthers', forward the message to the others. 89 | if ($message->command === 'sendMessageToOthers') { 90 | // To get the client sending this message, use the $event->from property. 91 | // To get a list of all connected clients, use the $event->clients pointer. 92 | $others = $event->allOtherClients(); 93 | foreach ($others as $client) { 94 | // The $message->data property holds the actual message 95 | $client->send('newMessage', $message->data); 96 | } 97 | } 98 | } 99 | 100 | public function onConnected(ClientConnected $event) 101 | { 102 | // Not used in this example. 103 | } 104 | 105 | public function onDisconnected(ClientDisconnected $event) 106 | { 107 | // Not used in this example. 108 | } 109 | 110 | /** 111 | * Register the listeners for the subscriber. 112 | * 113 | * @param Illuminate\Events\Dispatcher $events 114 | */ 115 | public function subscribe($events) 116 | { 117 | $events->listen( 118 | 'Codemash\Socket\Events\ClientConnected', 119 | 'App\Listeners\MessageEventListener@onConnected' 120 | ); 121 | 122 | $events->listen( 123 | 'Codemash\Socket\Events\MessageReceived', 124 | 'App\Listeners\MessageEventListener@onMessageReceived' 125 | ); 126 | 127 | $events->listen( 128 | 'Codemash\Socket\Events\ClientDisconnected', 129 | 'App\Listeners\MessageEventListener@onDisconnected' 130 | ); 131 | 132 | } 133 | } 134 | ``` 135 | 136 | What the application above does, is the following: a connected client sends a message with the `sendMessageToOthers` command, which basically forwards the message to the rest of the connected clients on your application. It is **important** to note that a client is not the same as a `User` model. A client is simply a connection from someones browser to your Laravel application, no matter if that user is authed or not. There is a possibility to fetch the connected authentication model, more on that later. 137 | 138 | Now it's time to write the client side, luckily the `Socket` facade takes care of that in no time. Create a blade template with the following content: 139 | 140 | ```html 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | {!! Socket::javascript() !!} 149 | 165 | 166 | 167 | ``` 168 | 169 | Finally, let's run the socket listener. You can do this by running the following artisan command in the project root: 170 | 171 | ```sh 172 | php artisan socket:listen 173 | ``` 174 | 175 | ## Client connections 176 | 177 | ### Using Eloquent models 178 | 179 | Laravel Socket reads the session when available and maps the `User` eloquent model to your client. You can then retrieve the Eloquent model by using the following code: 180 | 181 | ```php 182 | foreach ($clients as $client) { 183 | if ($client->authed()) { 184 | $user = $client->getUser(); 185 | // $user now holds the App\User model, 186 | // or the model set in the 'config.auth.providers.users.model' config variable. 187 | } 188 | } 189 | ``` 190 | 191 | Whenever you're using the clients list, like `$event->clients`, this is a Laravel Collection object. Methods such as filter, map, and so on, work very well on it. 192 | 193 | ### Storing client related data 194 | 195 | Client objects implement magic methods, meaning you can set any kind of additional properties during the life cycle of the server running. If you access a non existant property, it will however trigger an exception. Imagine you'd like to set a `connected_at` property containing the timestamp when the client connected. We'll add this property to the `onConnected` method in our event listener as seen below: 196 | 197 | ```php 198 | public function onConnected(ClientConnected $event) 199 | { 200 | $event->client->connected_at = Carbon::now(); 201 | } 202 | 203 | public function onMessageReceived(MessageReceived $event) 204 | { 205 | if ($event->message->command === 'whenDidIConnect') { 206 | if (isset($event->from->connected_at)) { 207 | $event->from->send('iConnectedAt', $event->from->connected_at->toString()); 208 | } 209 | } 210 | } 211 | ``` 212 | 213 | ## Production 214 | 215 | Ubuntu provides the neat `nohup` tool, which runs processes on the background. In case you'd like to run your socket on a production server and you're on Ubuntu, you may always use the nohup tool to run the socket listener. 216 | 217 | ```sh 218 | nohup php artisan socket:listen & 219 | ``` 220 | 221 | When using the `jobs` command, you'll see the socket running. It's easy to kill the process using the `kill ` command. The process ID is listed in the jobs list. 222 | 223 | ## Contributing 224 | 225 | If you're having problems, spot a bug, or have a feature suggestion, please log and issue on Github. If you'd like to have a crack yourself, fork the package and make a pull request. Any improvements are more than welcome. 226 | 227 | -------------------------------------------------------------------------------- /assets/Socket.js: -------------------------------------------------------------------------------- 1 | var Socket = function (url) { 2 | this.url = url; 3 | 4 | this.events = []; 5 | 6 | this.connected = false; 7 | 8 | this.triggerEvent = function (name, data, event) { 9 | var i = 0, len = this.events.length; 10 | for (; i < len; i++) { 11 | if (this.events[i].name === name) { 12 | this.events[i].callback(data, event); 13 | } 14 | } 15 | }; 16 | 17 | this.addEvent = function (name, callback) { 18 | this.events.push({ 19 | name: name, 20 | callback: callback 21 | }); 22 | }; 23 | 24 | this.removeEvent = function (callback) { 25 | var i = 0, len = this.events.length; 26 | for (; i < len; i++) { 27 | if (!this.events[i]) 28 | continue; 29 | if (this.events[i].callback === callback) { 30 | this.events.splice(i, 1); 31 | } 32 | } 33 | }; 34 | 35 | this.bindConnection = function () { 36 | var _this = this; 37 | 38 | this.connection.onopen = function (event) { 39 | _this.connected = true; 40 | 41 | _this.triggerEvent('connected', null, event); 42 | }; 43 | 44 | this.connection.onmessage = function (event) { 45 | _this.triggerEvent('message', null, event); 46 | 47 | var incoming = JSON.parse(event.data); 48 | _this.triggerEvent(incoming.command, incoming.data, event); 49 | }; 50 | 51 | this.connection.onclose = function (event) { 52 | _this.connected = false; 53 | 54 | _this.triggerEvent('disconnected', null, event); 55 | }; 56 | }; 57 | }; 58 | 59 | Socket.prototype.connect = function (callback) { 60 | if (callback) { 61 | this.bind('connected', callback); 62 | } 63 | 64 | this.connection = new WebSocket(this.url); 65 | this.bindConnection(); 66 | }; 67 | 68 | Socket.prototype.isConnected = function () { 69 | return this.connected; 70 | }; 71 | 72 | Socket.prototype.disconnect = function () { 73 | this.connection.close(); 74 | }; 75 | 76 | Socket.prototype.bind = function (name, callback) { 77 | this.addEvent(name, callback); 78 | return callback; 79 | }; 80 | 81 | Socket.prototype.on = Socket.prototype.bind; 82 | 83 | Socket.prototype.unbind = function (callback) { 84 | this.removeEvent(callback); 85 | }; 86 | 87 | Socket.prototype.send = function (command, data) { 88 | var json = JSON.stringify({ 89 | command: command, 90 | data: data 91 | }); 92 | this.connection.send(json); 93 | }; 94 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "codemash/laravel-socket", 3 | "description": "A socket implementation for Laravel 5", 4 | "version": "1.0.0", 5 | "require": { 6 | "cboden/ratchet": "^0.3.5" 7 | }, 8 | "license": "MIT", 9 | "authors": [ 10 | { 11 | "name": "Matthias Van Parijs", 12 | "email": "matvp91@gmail.com" 13 | } 14 | ], 15 | "autoload": { 16 | "psr-4": { 17 | "Codemash\\Socket\\": "src/" 18 | } 19 | }, 20 | "post-update-cmd": [ 21 | "php artisan vendor:publish" 22 | ] 23 | } 24 | -------------------------------------------------------------------------------- /config/socket.php: -------------------------------------------------------------------------------- 1 | 8043 6 | 7 | ]; 8 | -------------------------------------------------------------------------------- /src/Client.php: -------------------------------------------------------------------------------- 1 | connection = $connection; 60 | 61 | $this->session_manager = $session_manager; 62 | 63 | $this->ip = $connection->remoteAddress; 64 | 65 | // Check if client is behind a proxy. 66 | if (!$this->ip) { 67 | $this->ip = $connection->WebSocket->request->getHeader('X-Forwarded-For'); 68 | } 69 | 70 | // Handle session. 71 | $this->session_manager->handle(); 72 | 73 | $this->id = $connection->resourceId; 74 | } 75 | 76 | 77 | /** 78 | * Sets a data attribute. 79 | * 80 | * @return void 81 | */ 82 | public function __set($name, $value) 83 | { 84 | $this->data[$name] = $value; 85 | } 86 | 87 | 88 | /** 89 | * Checks whether an attribute is set or not. 90 | * 91 | * @return bool 92 | */ 93 | public function __isset($name) 94 | { 95 | return isset($this->data[$name]); 96 | } 97 | 98 | 99 | /** 100 | * Gets a data attribute. 101 | * 102 | * @return mixed 103 | */ 104 | public function __get($name) 105 | { 106 | $identifier = 'get' . camel_case($name) . 'Attribute'; 107 | 108 | if (method_exists($this, $identifier)) { 109 | return call_user_func([$this, $identifier]); 110 | } 111 | 112 | if (isset($this->data[$name])) { 113 | return $this->data[$name]; 114 | } 115 | 116 | throw new Exception('Undefined property via __get(): ' . $name); 117 | } 118 | 119 | 120 | /** 121 | * Sends a message to this client. 122 | * 123 | * @return void 124 | */ 125 | public function send($command, $data) 126 | { 127 | $message = new Message; 128 | 129 | $message->command = $command; 130 | $message->data = $data; 131 | 132 | $this->connection->send($message->serialize()); 133 | } 134 | 135 | 136 | /** 137 | * Hooks in the user getter. 138 | * 139 | * @return mixed 140 | */ 141 | public function getUser() { 142 | $session = $this->session_manager->session; 143 | 144 | if ($session && !$this->user) { 145 | $user_id = $session->get(Auth::getName()); 146 | 147 | if ($user_id && is_null($this->user)) { 148 | $user_model = Config::get('auth.providers.users.model'); 149 | 150 | if ($user_model) { 151 | $this->user = $user_model::find($user_id); 152 | } 153 | } else { 154 | $this->user = null; 155 | } 156 | } 157 | 158 | return $this->user; 159 | } 160 | 161 | 162 | /** 163 | * Check if user is authenticated. 164 | * 165 | * @return bool 166 | */ 167 | public function authed() { 168 | $this->getUser(); 169 | 170 | return !is_null($this->user); 171 | } 172 | 173 | } 174 | -------------------------------------------------------------------------------- /src/Clients.php: -------------------------------------------------------------------------------- 1 | first(function ($client, $key) use ($connection) { 17 | return $client->connection === $connection; 18 | }); 19 | } 20 | 21 | /** 22 | * Adds a client connection. 23 | */ 24 | public function add($client) { 25 | $this->items[$client->id] = $client; 26 | } 27 | 28 | /** 29 | * Removes a client connection. 30 | */ 31 | public function remove($client) { 32 | $this->offsetUnset($client->id); 33 | } 34 | 35 | /** 36 | * Gets only the authed users. 37 | */ 38 | public function authedClients() 39 | { 40 | return $this->filter(function ($client) { 41 | return $client->authed(); 42 | }); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/Commands/Listen.php: -------------------------------------------------------------------------------- 1 | option('port'); 31 | 32 | if ($port && !is_numeric($port)) { 33 | $this->error('Port must be numeric'); 34 | return; 35 | } 36 | 37 | $server = new Server($this, $port); 38 | 39 | $this->info('Running on ' . $server->getUrl(false)); 40 | 41 | $server->run(); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/Events/ClientConnected.php: -------------------------------------------------------------------------------- 1 | port = $server->getPort(); 41 | 42 | $this->clients = $clients; 43 | 44 | $this->client = $client; 45 | } 46 | 47 | /** 48 | * Get all users connected. 49 | * 50 | * @var array 51 | */ 52 | public function allOtherClients() 53 | { 54 | $current_client = $this->client; 55 | 56 | $filter = function ($client) use ($current_client) { 57 | return $client !== $current_client; 58 | }; 59 | 60 | return $this->clients->filter($filter); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/Events/MessageReceived.php: -------------------------------------------------------------------------------- 1 | from = $client; 33 | 34 | $this->message = $message; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/Facades/Socket.php: -------------------------------------------------------------------------------- 1 | command = $data['command']; 32 | $this->data = (object) array_get($data, 'data'); 33 | } 34 | } 35 | 36 | 37 | /** 38 | * Serializes the message. 39 | * 40 | * @var string 41 | */ 42 | public function serialize() 43 | { 44 | return json_encode([ 45 | 'command' => $this->command, 46 | 'data' => $this->data 47 | ]); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/MessageComponent.php: -------------------------------------------------------------------------------- 1 | clients = new Clients; 37 | 38 | $this->command = $server->getCommand(); 39 | 40 | $this->server = $server; 41 | } 42 | 43 | 44 | /** 45 | * Client connected. 46 | */ 47 | public function onOpen(ConnectionInterface $connection) 48 | { 49 | // Resolve the session upon connection. 50 | $session_manager = new SessionManager($connection); 51 | $session_manager->resolveSession(); 52 | 53 | // Create the client and add it. 54 | $client = new Client($connection, $session_manager); 55 | $this->clients->add($client); 56 | 57 | $this->command->info('Client connected'); 58 | 59 | // Fire the event. 60 | Event::fire(new ClientConnected($this->server, $this->clients, $client)); 61 | } 62 | 63 | 64 | /** 65 | * Incoming message. 66 | */ 67 | public function onMessage(ConnectionInterface $from, $message) 68 | { 69 | $incoming_client = $this->clients->findByConnection($from); 70 | 71 | if ($incoming_client) { 72 | // Handle session. 73 | // $incoming_client->session_manager->handle(); 74 | 75 | // Handle message. 76 | $message = new Message($message); 77 | 78 | // Fire the event. 79 | Event::fire(new MessageReceived($this->server, $this->clients, $incoming_client, $message)); 80 | } 81 | } 82 | 83 | 84 | /** 85 | * Client disconnected. 86 | */ 87 | public function onClose(ConnectionInterface $connection) 88 | { 89 | $client = $this->clients->findByConnection($connection); 90 | 91 | if ($client) { 92 | $this->clients->remove($client); 93 | 94 | $this->command->info('Client disconnected'); 95 | 96 | // Fire the event. 97 | Event::fire(new ClientDisconnected($this->server, $this->clients, $client)); 98 | } 99 | } 100 | 101 | 102 | /** 103 | * Socket error. 104 | */ 105 | public function onError(ConnectionInterface $connection, Exception $e) 106 | { 107 | $this->command->error($e->getMessage()); 108 | 109 | \Log::error($e); 110 | 111 | $connection->close(); 112 | } 113 | 114 | } 115 | -------------------------------------------------------------------------------- /src/Server.php: -------------------------------------------------------------------------------- 1 | command = $command; 42 | 43 | if (is_null($port)) { 44 | $port = Config::get('socket.default_port'); 45 | } 46 | 47 | if (is_null($message_component)) { 48 | $message_component = new MessageComponent($this); 49 | } 50 | 51 | $this->port = $port; 52 | 53 | $http_server = new HttpServer(new WsServer($message_component)); 54 | $this->io = IoServer::factory($http_server, $port); 55 | } 56 | 57 | 58 | /** 59 | * Run the server. 60 | * 61 | * @return void 62 | */ 63 | public function run() 64 | { 65 | $this->io->run(); 66 | } 67 | 68 | 69 | /** 70 | * Gets the IO interface. 71 | * 72 | * @return IoServer 73 | */ 74 | public function getIO() 75 | { 76 | return $this->io; 77 | } 78 | 79 | 80 | /** 81 | * Gets the port. 82 | * 83 | * @return int 84 | */ 85 | public function getPort() 86 | { 87 | return $this->port; 88 | } 89 | 90 | 91 | /** 92 | * Constructs and gets the endpoint URL. 93 | * 94 | * @return string 95 | */ 96 | public function getUrl($include_scheme = true) 97 | { 98 | $parts = parse_url(url('/')); 99 | $scheme = ''; 100 | if ($include_scheme) { 101 | $scheme = 'ws://'; 102 | } 103 | return $scheme . $parts['host'] . ':' . $this->port; 104 | } 105 | 106 | 107 | /** 108 | * Gets the command used to trigger this server. 109 | * 110 | * @return Illuminate\Console\Command 111 | */ 112 | public function getCommand() 113 | { 114 | return $this->command; 115 | } 116 | 117 | } 118 | -------------------------------------------------------------------------------- /src/SessionManager.php: -------------------------------------------------------------------------------- 1 | connection = $connection; 35 | 36 | $app = App::getInstance(); 37 | $session_manager = new SessionHandler($app); 38 | $this->session = $session_manager->driver(); 39 | } 40 | 41 | 42 | /** 43 | * Gets a cookie by the name and if needed, can decrypt the value. 44 | * 45 | * @return string|null 46 | */ 47 | private function getCookie($name, $decrypt = false) 48 | { 49 | try { 50 | $cookies = $this->connection->WebSocket->request->getCookies(); 51 | 52 | if (isset($cookies[$name])) { 53 | $raw = rawurldecode($cookies[$name]); 54 | 55 | if (!$decrypt) { 56 | return $raw; 57 | } 58 | 59 | $decrypted = @Crypt::decrypt($raw); 60 | 61 | if ($decrypted) { 62 | return $decrypted; 63 | } 64 | } 65 | 66 | return null; 67 | } catch (Exception $ex) { 68 | \Log::error($ex); 69 | 70 | return null; 71 | } 72 | } 73 | 74 | 75 | /** 76 | * Resolves the laravel session and sets the session. 77 | * 78 | * @return mixed 79 | */ 80 | public function resolveSession() 81 | { 82 | $cookie_name = Config::get('session.cookie'); 83 | $session_id = $this->getCookie($cookie_name, true); 84 | 85 | if (!$session_id) { 86 | return null; 87 | } 88 | 89 | $this->session->setId($session_id); 90 | 91 | return $this->session; 92 | } 93 | 94 | 95 | /** 96 | * Checks if we have a session set. 97 | * 98 | * @return bool 99 | */ 100 | public function hasSession() 101 | { 102 | return !is_null($this->session); 103 | } 104 | 105 | 106 | /** 107 | * Starts the session. 108 | * 109 | * @return void 110 | */ 111 | public function start() 112 | { 113 | $this->session->start(); 114 | } 115 | 116 | 117 | /** 118 | * Handles the session. 119 | * 120 | * @return void 121 | */ 122 | public function handle() 123 | { 124 | if ($this->hasSession()) { 125 | $this->start(); 126 | } 127 | } 128 | 129 | } 130 | -------------------------------------------------------------------------------- /src/Socket.php: -------------------------------------------------------------------------------- 1 | ', 26 | '' 27 | ]); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/SocketServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 26 | __DIR__ . '/../config/socket.php' => config_path('socket.php') 27 | ]); 28 | 29 | $this->publishes([ 30 | __DIR__ . '/../assets/Socket.js' => public_path('vendor/socket/socket.js') 31 | ], 'public'); 32 | } 33 | 34 | 35 | /** 36 | * Register the application services. 37 | * 38 | * @return void 39 | */ 40 | public function register() 41 | { 42 | $this->commands($this->commands); 43 | 44 | $this->app->bind('codemash.socket', function () { 45 | return new Socket; 46 | }); 47 | } 48 | 49 | 50 | /** 51 | * Provides bindings to the application. 52 | * 53 | * @return array 54 | */ 55 | public function provides() 56 | { 57 | return []; 58 | } 59 | 60 | } 61 | --------------------------------------------------------------------------------