Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
composer require predis/predis npm install --save express socket.io ioredis php-unserialize dotenv lodash php artisan make:event MessageCreatedEvent
- Loading branch information
Showing
12 changed files
with
270 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace App\Events; | ||
|
||
use App\Events\Event; | ||
use Illuminate\Queue\SerializesModels; | ||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast; | ||
|
||
class MessageCreatedEvent extends Event implements ShouldBroadcast | ||
{ | ||
use SerializesModels; | ||
|
||
public $destinations = []; | ||
public $message; | ||
|
||
/** | ||
* Create a new event instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct($destinations, $message) | ||
{ | ||
$this->destinations = $destinations; | ||
$this->message = $message; | ||
} | ||
|
||
/** | ||
* Get the channels the event should be broadcast on. | ||
* | ||
* @return array | ||
*/ | ||
public function broadcastOn() | ||
{ | ||
return ['chat']; | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace App\Observers; | ||
|
||
use App\Message; | ||
|
||
class MessageObserver { | ||
|
||
public function saving($model) | ||
{ | ||
// | ||
} | ||
|
||
public function saved(Message $model) | ||
{ | ||
$messages = \App\Message::with('fromUser')->with('toUser') | ||
->where('id', $model->id)->get()->toArray(); | ||
event(new \App\Events\MessageCreatedEvent([$model->from_user_id, $model->to_user_id], current($messages))); | ||
} | ||
|
||
} |
This file contains 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
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
require('dotenv').config({ | ||
path: __dirname +'/.env' | ||
}); | ||
|
||
const env = process.env; | ||
const port = env.NODE_SERVER_PORT; | ||
|
||
const _ = require('lodash'); | ||
const redis = require('ioredis'); | ||
const redisClient = new redis(); | ||
const redisBroadcast = new redis(); | ||
const cookie = require('cookie'); | ||
const crypto = require('crypto'); | ||
const PHPUnserialize = require('php-unserialize'); | ||
|
||
const server = require('http').createServer(); | ||
console.log('Server on Port : ' + port); | ||
server.listen(port); | ||
const io = require('socket.io')(server); | ||
|
||
io.use(middlewareAuthCheck); | ||
|
||
io.on('connection', (socket) => { | ||
socket.on('user', (user) => { | ||
user = JSON.parse(user); | ||
console.log('user', user); | ||
// mark with userId | ||
socket.userId = user.id; | ||
}); | ||
|
||
socket.on('disconnect', () => { | ||
console.log('disconnect..'); | ||
}); | ||
}); | ||
|
||
redisBroadcast.psubscribe('*', (err, count) => { | ||
}); | ||
|
||
redisBroadcast.on('pmessage', (subscribed, channel, event) => { | ||
event = JSON.parse(event); | ||
console.log('pmessage', channel, event); | ||
|
||
const _sockets = _.toArray(io.sockets.connected); | ||
event.data.destinations.forEach((userId) => { | ||
const socket = _.find(_sockets, (_socket) => { | ||
if (+userId === +_socket.userId) { | ||
return true; | ||
} | ||
}); | ||
if (socket) { | ||
console.log('emit', socket.id, userId); | ||
io.to(socket.id).emit('chat', event.data.message); | ||
} | ||
}); | ||
}); | ||
|
||
function middlewareAuthCheck(socket, next) { | ||
|
||
try{ | ||
|
||
var _cookie = socket.request.headers.cookie; | ||
if(!_cookie){ | ||
throw new Error('No cookie'); | ||
} | ||
_cookie = cookie.parse(_cookie); | ||
if(!_cookie || !_cookie['laravel_session']){ | ||
throw new Error('No valid cookie'); | ||
} | ||
|
||
const sessionId = decryptCookie(_cookie['laravel_session']); | ||
console.log('sessionId', sessionId); | ||
|
||
redisClient.get('laravel:' + sessionId, (err, session) => { | ||
|
||
if (err) { | ||
next(err); | ||
} else if (session) { | ||
next(); | ||
} else { | ||
next(new Error('No session in redis')); | ||
} | ||
|
||
}); | ||
|
||
}catch(err){ | ||
console.log('Skip this socket since an error occurred', err); | ||
next(err); | ||
} | ||
|
||
}; | ||
|
||
function decryptCookie(cookie) { | ||
console.log('cookie', cookie); | ||
const parsedCookie = JSON.parse(new Buffer(cookie, 'base64')); | ||
|
||
const iv = new Buffer(parsedCookie.iv, 'base64'); | ||
const value = new Buffer(parsedCookie.value, 'base64'); | ||
|
||
var appKey = env.APP_KEY; | ||
|
||
if (/base64/.test(env.APP_KEY)) { | ||
const matches = /base64:(.+)/.exec(appKey); | ||
console.log('appKey', matches[1]); | ||
appKey = new Buffer(matches[1], 'base64').toString(); | ||
console.log('appKey', appKey); | ||
// TODO: Error in crypto.createDecipheriv if appKey is encoded in base64 | ||
// appKey seems to be binary | ||
// https://github.com/laravel/framework/commit/370ae34d41362c3adb61bc5304068fb68e626586 | ||
} | ||
|
||
const decipher = crypto.createDecipheriv('aes-256-cbc', appKey, iv); | ||
const resultSerialized = Buffer.concat([ | ||
decipher.update(value), | ||
decipher.final() | ||
]); | ||
|
||
return PHPUnserialize.unserialize(resultSerialized); | ||
}; |