Monday 1 May 2017

Socket.io on connect not working

I've seen this problem before and I've read through all the docs I can find, but somehow I'm just not seeing it in my code. It must be something very trivial I am getting wrong, but any help would be appreciated.

The issue is that I can't get socket.io to connect to the server. On the server side the 'connection established' shows up, but none of my other console logs on server or client sides. I've followed the documentation of the socket.io folks pretty closely.

Here is the server side (in node.js/express btw):

module.exports = function(server){
  console.log('what is server', server);
  var io = require('socket.io')(server);
  var ss = require('socket.io-stream');
  var fs = require('fs');
  var serveIndex = require('serve-index');
  var path = require('path');


io.on('connection', function(socket) {
  console.log('connection established');
  io.sockets.on('songName', function(data) {
    console.log('in back side socket.on');
    var stream = ss.createStream();
    var filename = serveIndex('/playlist/', path.basename(data.name), '.mp3');
    res.writeHead(200, {
      'Content-Type': 'audio/mpeg' || 'audio/mp4',
      'Content-Length': stat.size
    });
    ss(socket).emit('audioStream', stream, { name: filename });
    fs.createReadStream(filename).pipe(stream);
  });
});
};

And here is the client side (and sanity check2 console log fires):

console.log('sanity check2');

$(document).ready(function(){

  window.AudioContext = window.AudioContext||window.webkitAudioContext;
    context = new AudioContext();

  var socket = io.connect('http://localhost:5000/socket');

  socket.on('connection', function(){
    console.log('front end socket connected succesfully');
    socket.emit('songName', {name: 'LaNegra'});
  })

  socket.on('audioStream', function(stream) {
    console.log('in front side socket.on');
    var source = context.createMediaStreamSource(stream);
    source.connect(context.destination);
  });


});

Again, this is a pretty simple problem most likely, so I apologize in advance.



via Peter Weyand

No comments:

Post a Comment