我仍然是socket.io的新手,我试图将一个值传递给服务器端并将其存储在全局var中,然后我可以使用它来对ARI做一些逻辑。
所以在我的服务器端,我有:
io.sockets.on('muting', function (data) {
mute = data;
console.log("client side:" + mute);
});
为清晰起见,整个服务器端代码:
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);
}
在我的客户端:
$(document).on('click', '#kick', function() {
mute = !mute;
socket.emit('muting', mute);
console.log(mute)
});
完整的客户端代码:
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)
});
});
我错过了一些非常小的东西,却无法弄明白。
编辑:我没有收到错误消息,似乎根本没有通过信息服务器端。使用快递。
亲切的问候。
答案 0 :(得分:1)
来自socket io doc:http://socket.io/docs/#using-with-the-express-framework
您可以尝试将'muting'
事件侦听器包装在'connection'
事件侦听器中。请注意,您将使用socket
事件中的'connection'
参数来收听'muting'
服务器端:
io.sockets.on('connection',function (socket) {
updateSip();
socket.on('muting', function (data) {
mute = data;
console.log("client side:" + mute);
});
});
答案 1 :(得分:0)
var socket = io.connect();
不,你没有到达你的服务器!
//Socket.io logic here
server.listen(3009, function () {
console.log('listening on *:3009');
});
您的套接字服务器似乎在端口3009上进行侦听,因此您必须告诉您的客户端您的服务器在哪里(路径 - &gt; ex:http://myoffice.localhost)以及到达服务器的端口
尝试使用以下方式更新客户端:
io.connect("wss://" + document.location.hostname + ':' + 3009);