I need to pass the value of the PHPSESSID cookie when a client try to connect to a socket.io websocket.
I tried to use socket.handshake.headers
, but for some reason the PHPSESSID was not passed.
I tried to emit a connect event to send the cookie value. But I still can't seems to be able to do this.
Here is what I have done
This is my websocket server code
var env = require('./config');
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var clients = [];
server.listen(env.socket.port, env.socket.host, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Websocket running at http://%s:%s', host, port);
});
app.get('/', function (req, res) {
res.send('Welcome!');
});
io.on('connection', function (socket) {
//console.log(socket.handshake.headers);
clients.push(socket);
socket.on('connect', function(msg){
console.log('PHPSESSID: ' + msg);
});
socket.on('chat', function(msg){
console.log('message: ' + msg);
});
socket.emit('chat', { hello: 'world' });
});
Here is my client code
<script>
$(function(){
var socket = io.connect('https://10.0.4.18:8020');
socket.emit('connect', { 'sessionId' : '<?php echo $_COOKIE['PHPSESSID']; ?>' });
$('#f').click(function(e){
e.preventDefault();
if($('#m').val() == '')
return;
socket.emit('chat', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat', function(msg){
$('#messages').append($('<li>').text(msg));
});
});
</script>
I am expecting to print the json string with the sessionId to the server's console.
How can I pass/access the PHPSESSID value from the websocket?