Last active
March 3, 2016 20:44
-
-
Save Carlsson87/245b226a955a750d03fd to your computer and use it in GitHub Desktop.
PHP + NodeJS + Socket.io
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
use GuzzleHttp\Client; | |
$client = new Client([ | |
'base_uri' => '127.0.0.1:8001', | |
]); | |
$client->request('POST', 'http://httpbin.org/post', [ | |
'body' => 'raw data' | |
]); |
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 socket = io('http://127.0.0.1:8001'); | |
socket.emit('joining', 'a_room'); | |
socket.on('message', function (data) { | |
console.log(data); | |
}); | |
send = function(message) { | |
socket.emit('message', message); | |
} |
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 app = require('http').createServer(handler) | |
var io = require('socket.io')(app); | |
app.listen(8001); | |
io.on('connection', function (socket) { | |
socket.on('joining', function(room) { | |
socket.room = room; | |
socket.join(room); | |
}); | |
socket.on('message', function(message) { | |
socket.broadcast.to(room).emit('message', message); | |
}); | |
}); | |
function handler (req, res) { | |
req.on('data', function(chunk) { | |
io.sockets.in('a_room').emit('message', JSON.parse(chunk.toString())); | |
}); | |
res.writeHead(200); | |
res.end("Message sent"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment