Studento919
Studento919

Reputation: 635

Pass variable value to client side and store it in global variable

Am still new to socket.io, Am trying to pass a value to the server side and store it in a global var which I can then use to do some logic with ARI.

So on my server side I have:

io.sockets.on('muting', function (data) {
    mute = data;
    console.log("client side:" + mute);
});

Entire server side code for clarity:

var ari = require('ari-client');
var util = require('util');
var chanArr = [];
var test;
var mute;
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);

//ARI client
ari.connect('http://localhost:8088', 'asterisk', 'asterisk', clientLoaded);

function clientLoaded(err, client) {
    if (err) {
        throw err;
    }
    // find or create a holding bridges
    var bridge = null;
    client.bridges.list(function (err, bridges) {
        if (err) {
            throw err;
        }

        bridge = bridges.filter(function (candidate) {
                return candidate.bridge_type === 'mixing';
            })[0];

        if (bridge) {
            console.log(util.format('Using bridge %s', bridge.id));
        } else {
            client.bridges.create({
                type : 'mixing'
            }, function (err, newBridge) {
                if (err) {
                    throw err;
                }

                bridge = newBridge;
                console.log(util.format('Created bridge %s', bridge.id));
            });
        }
    });

    // handler for StasisStart event
    function stasisStart(event, channel) {
        console.log(util.format(
                'Channel %s just entered our application, adding it to bridge %s',
                channel.name,
                bridge.id));

        channel.answer(function (err) {
            if (err) {
                throw err;
            }

            bridge.addChannel({
                channel : channel.id
            }, function (err) {
                var id = chanArr.push(channel.name)
                    console.log("Value: " + test);
                test = channel.name;
                updateSip);

                if (err) {
                    throw err;
                }

                //If else statement to start music for first user entering channel, music will stop once more than 1 enters the channel.
                if (chanArr.length <= 1) {
                    bridge.startMoh(function (err) {
                        if (err) {
                            throw err;
                        }
                    });
                } else if (chanArr.length === 2) {
                    bridge.stopMoh(function (err) {
                        if (err) {
                            throw err;
                        }
                    });
                } else {}

            });

        });

    }

    // handler for StasisEnd event
    function stasisEnd(event, channel) {
        console.log(util.format(
                'Channel %s just left our application', channel.name));
        console.log(channel.name);

        var index = chanArr.indexOf(channel.name);
        chanArr.splice(index, 1);
        updateSip();
    }
    client.on('StasisStart', stasisStart);
    client.on('StasisEnd', stasisEnd);
    client.start('bridge-hold');
}

//Socket.io logic here
server.listen(3009, function () {
    console.log('listening on *:3009');
});

app.use(express.static(__dirname + '/public'));
app.get('/', function (req, res) {
    res.sendfile(__dirname + "/testPage.html");
});

io.sockets.on('connection', function (data) {
    updateSip();
});

io.sockets.on('muting', function (data) {
    mute = data;
    console.log("client side:" + mute);
});

function updateSip() {
    console.log("Value: " + test);
    io.sockets.emit('sip', chanArr);
}

And on my client side:

    $(document).on('click', '#kick', function() {
        mute = !mute;
        socket.emit('muting', mute);
        console.log(mute)
});

Full client side code:

jQuery(function ($) {
    var socket = io.connect();
    var mute = false;
    var $sip = $('#sip');

    socket.on('sip', function (data) {
        var sip = '';
        $(".exe").remove();
        for (i = 0; i < data.length; i++) {
            sip += data[i];
            if (sip) {
                $sip.append('<tr class="exe">\
                                                        <td>' + sip + '</td>\
                                                        <td><button class="btn btn-default mute" id="kick" type="submit">Mute</button></td>\
                                                        <td><button class="btn btn-default kick" id="kicks" data-toggle="modal" data-target="#myModal" type="submit">Kick</button></td>\
                                                        </tr>');
            } else {
                $sip.append('Currently no extensions');
            }
            sip = '';
        }

    });

    $('.kick').click(function () {
        $('#myInput').focus()
    });

    $(document).on('click', '#kick', function () {
        mute = !mute;
        socket.emit('muting', mute);
        console.log(mute)
    });

});

Am missing something very small, yet cant figure it out.

EDIT: I am not getting error messages, seems am not passing the information server side at all for some reason.

Am using express.

Kind regards.

Upvotes: 1

Views: 1262

Answers (2)

Below the Radar
Below the Radar

Reputation: 7635

From socket io doc: http://socket.io/docs/#using-with-the-express-framework

You could try to wrap you 'muting' event listener in the 'connection' event listener. Note that you will use the socket parameter from the 'connection' event to listen to 'muting'

server side:

io.sockets.on('connection',function (socket) {
  updateSip();
  socket.on('muting', function (data) {
     mute = data;
     console.log("client side:" + mute);
  });
});

Upvotes: 1

Anonymous0day
Anonymous0day

Reputation: 3042

var socket = io.connect();

No you are not reaching your server !

 //Socket.io logic here
 server.listen(3009, function () {
     console.log('listening on *:3009');
 });

Your socket server seem to be listenning on port 3009, so you have to tell your client where is your server (the path -> ex : http://myoffice.localhost ) and the port to reach the server

try to update client side with :

 io.connect("wss://" + document.location.hostname + ':' + 3009);

Upvotes: 0

Related Questions