Last active
May 18, 2016 09:13
-
-
Save hkan/986cebaa32c427b6952d to your computer and use it in GitHub Desktop.
Socket.io + Laravel 5.1 (Late edit: This is shit. Do not use. Use JWT instead.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use App\User; | |
use Illuminate\Console\Command; | |
class CheckSessionForNodeSocket extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'session:check {--hash=} {--id=}'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Command description.'; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() | |
{ | |
if ($this->isHashIncorrect()) | |
{ | |
$this->line('hash is incorrect'); | |
return 1; | |
} | |
/** @var User $user */ | |
$user = User::find($this->option('id')); | |
// data to ouput | |
$data = [ | |
'channels' => [] | |
]; | |
echo json_encode($data); | |
return 0; | |
} | |
private function isHashIncorrect() | |
{ | |
return ! app('hash')->check(date('Y-m-d') . $this->option('id'), $this->option('hash')); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var exec = require('child_process').exec; | |
var _ = require('underscore'); | |
var app = require('express')(); | |
var http = require('http').Server(app); | |
var io = require('socket.io')(http); | |
var Redis = require('ioredis'); | |
var redis = new Redis(); | |
var artisan = require('./misc/artisan.js'); | |
var ClientList = {}; | |
// Socket.IO middleware for authentication | |
io.use(function (socket, next) { | |
var params = socket.handshake.query; | |
exec('php artisan session:check --hash="' + params.hash.replace(/\$/gm, '\\$') + '" --id=' + params.id, function (error, stdout, stderr) { | |
if (error !== null) { | |
socket.disconnect(); | |
next(new Error('Could not authenticate')); | |
} else { | |
try { | |
var response = JSON.parse(stdout); | |
_.each(response.channels, function (channel) { | |
socket.join(channel); | |
}); | |
} catch (e) {} | |
// store socket ID | |
ClientList[params.id] = socket.id; | |
next(); | |
} | |
}); | |
}); | |
var channels = { | |
'default': function (channel, message) { | |
io.emit(message.event, message.data); | |
}, | |
'user.*': function (channel, message) { | |
var userID = channel.match(/^user\.(\d+)$/i)[1]; | |
io.to(ClientList[userID]).emit(message.event, message.data); | |
} | |
}; | |
var redisOnSubscribe = function (err, count) { | |
if (err !== null) { | |
console.log('- could not subscribe to redis', err); | |
} | |
}; | |
// Tell Redis that we are here | |
// redis.psubscribe('channel1', 'channel2', ..., redisOnSubscribe); | |
redis.psubscribe.apply(redis, _.union([], _.keys(channels), [redisOnSubscribe])); | |
// Listen to Redis | |
redis.on('pmessage', function (pattern, channel, message) { | |
message = JSON.parse(message); | |
channels[pattern].call(channels, channel, message); | |
}); | |
http.listen(3000, function () {}); | |
/* | |
|-------------------------------------------------------------------------- | |
| Client events | |
|-------------------------------------------------------------------------- | |
*/ | |
io.on('connection', function(socket) { | |
var userID = _.invert(ClientList)[socket.id]; | |
socket.on('connection-succeeded', function () { | |
artisan('socket:connected ' + userID); | |
}); | |
socket.on('notifications.marked-read', function (notif, callback) { | |
artisan('notifications:mark-read ' + userID + ' ' + notif, callback); | |
}); | |
socket.on('conversation.message', function (data) { | |
artisan('conversation:message ' + userID + ' --conversation=' + data.conversation + ' --message="' + encodeURI(data.message) + '" --identifier="' + data.identifier + '"'); | |
}); | |
socket.on('conversation.read', function (data, callback) { | |
artisan('conversation:read ' + userID + ' --conversation=' + data.conversation, callback); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var exec = require('child_process').exec; | |
module.exports = function (cmd, callback) { | |
exec('php artisan ' + cmd, function (error, stdout) { | |
// Command output | |
var result = stdout; | |
// Try to parse it as JSON | |
try { | |
result = JSON.parse(stdout); | |
} catch (e) {} | |
if (typeof callback == 'function') { | |
callback(error !== null, result); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment