Permalink
Browse files

Socket gun

  • Loading branch information...
1 parent 8917019 commit 736f366b0c1e24755b898e852e072b94b5419390 @marcus-campos committed Jan 10, 2017
Showing with 222 additions and 2 deletions.
  1. +24 −2 README.md
  2. +22 −0 composer.json
  3. +29 −0 config/socket.php
  4. +41 −0 src/Emitter/Emitter.php
  5. +19 −0 src/Facades/Emitter.php
  6. +35 −0 src/Receiver/Receiver.php
  7. +52 −0 src/SocketServiceProvider.php
View
@@ -1,2 +1,24 @@
-# laravel-socket
-Socket gun based on Elephant.io for Laravel.
+# Laravel Socket.io
+Socket gun based on wisembly/socket.io(socket.io v1.* emitter only) for Laravel.
+
+Instalation
+####Run
+```
+composer require marcuscampos/laravel-socket
+```
+####Add to config/app.php providers
+```
+MarcusCampos\LaravelSocket\SocketServiceProvider::class,
+```
+If you want custom name add to aliases
+```
+'SocketIo' => \MarcusCampos\LaravelSocket\Emitter\Facades\Emitter::class,
+```
+####Publish config
+```
+php artisan vendor:publish
+```
+####Usage
+```
+Emitter::emit('channelOrEvent', array('foo' => 'bar'));
+```
View
@@ -0,0 +1,22 @@
+{
+ "name": "marcuscampos/laravel-socket",
+ "description": "Socket gun based on Elephant.io for Laravel",
+ "license": "MIT",
+ "keywords": ["socket", "laravel", "react", "elephant", "io"],
+ "homepage": "https://github.com/marcus-campos/laravel-socket",
+ "authors": [
+ {
+ "name": "Marcus Vinícius Campos",
+ "email": "campos.v.marcus@gmail.com"
+ }
+ ],
+ "autoload": {
+ "psr-4": {
+ "MarcusCampos\\LaravelSocket\\": "src/"
+ }
+ },
+ "require": {
+ "wisembly/elephant.io": "^3.2"
+ },
+ "minimum-stability": "dev"
+}
View
@@ -0,0 +1,29 @@
+<?php
+
+
+/*
+ * This file is part of ZukA Laravel.
+ *
+ * (c) Marcus Campos <marcus_vinicius_campos@hotmail.com>
+ *
+ */
+
+return [
+
+ /*
+ |--------------------------------------------------------------------------
+ | Socket Configuration
+ |--------------------------------------------------------------------------
+ */
+
+ 'emit' => [
+ 'host' => 'http://localhost',
+ 'port' => '8000'
+ ],
+
+ 'listen' => [
+ 'host' => 'http://localhost',
+ 'port' => '8000'
+ ]
+
+];
@@ -0,0 +1,41 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: marcus
+ * Date: 09/01/17
+ * Time: 13:56
+ */
+
+namespace MarcusCampos\LaravelSocket\Emitter;
+
+use ElephantIO\Client;
+use ElephantIO\Engine\SocketIO\Version1X;
+use Config;
+use ElephantIO\Exception\ServerConnectionFailureException;
+
+class Emitter
+{
+ /**
+ * @param $channel
+ * @param $message
+ * @return bool
+ */
+
+ public static function emit($channel, $message){
+ try{
+
+ $host = config('socket.emit.host');
+ $port = config('socket.emit.port');
+
+ $url = $host.':'.$port;
+ $client = new Client(new Version1X($url));
+ $client->initialize();
+ $client->emit($channel, $message);
+ $client->close();
+ }catch (ServerConnectionFailureException $ex){
+ \Log::alert('failed to connect socket io stream on '.$url);
+ return false;
+ }
+ return true;
+ }
+}
@@ -0,0 +1,19 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: marcus
+ * Date: 09/01/17
+ * Time: 13:57
+ */
+
+namespace MarcusCampos\LaravelSocket\Facades;
+
+
+use Illuminate\Support\Facades\Facade;
+
+class Emitter extends Facade
+{
+ protected static function getFacadeAccessor(){
+ return 'emitter';
+ }
+}
@@ -0,0 +1,35 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: marcus
+ * Date: 10/01/17
+ * Time: 09:42
+ */
+
+namespace MarcusCampos\LaravelSocket\Receiver;
+
+use ElephantIO\Client;
+use ElephantIO\Engine\SocketIO\Version1X;
+use Config;
+use ElephantIO\Exception\ServerConnectionFailureException;
+
+class Receiver
+{
+ public static function receive()
+ {
+ $host = config('socket.emit.host');
+ $port = config('socket.emit.port');
+
+ $url = $host.':'.$port;
+
+ $client = new Client(new Version1X($url));
+ $client->initialize();
+
+ while (true) {
+ $r = $client->read();
+ dd($r);
+ }
+
+ $client->close();
+ }
+}
@@ -0,0 +1,52 @@
+<?php
+
+namespace MarcusCampos\LaravelSocket;
+
+use Illuminate\Foundation\AliasLoader;
+use Illuminate\Support\ServiceProvider;
+use Illuminate\Contracts\Container\Container as Application;
+use Illuminate\Foundation\Application as LaravelApplication;
+use MarcusCampos\LaravelSocket\Emitter\Emitter;
+
+class SocketServiceProvider extends ServiceProvider
+{
+ /**
+ * Bootstrap the application services.
+ *
+ * @return void
+ */
+ public function boot()
+ {
+ $this->setupConfig($this->app);
+ }
+
+ protected function setupConfig(Application $app)
+ {
+ $source = realpath(__DIR__.'/../config/socket.php');
+
+ if ($app instanceof LaravelApplication && $app->runningInConsole()) {
+ $this->publishes([$source => config_path('socket.php')]);
+ }
+ }
+
+ /**
+ * Register the application services.
+ *
+ * @return void
+ */
+ public function register()
+ {
+ $this->app['config']->get('socket');
+
+ $this->app['emmiter'] = $this->app->share(function ($app){
+ return new Emitter();
+ });
+
+ $this->app->booting(function(){
+ $loader = AliasLoader::getInstance();
+ $loader->alias('Emitter', 'MarcusCampos\LaravelSocket\Facades\Emitter');
+ });
+
+ }
+
+}

0 comments on commit 736f366

Please sign in to comment.