Socket.IO disconnecting first user in room - javascript

I am having trouble with having the first user of the room I am trying to communicate to always leaving but this only happens when I check for its id.
socket.on('player moved', function(msg) {
var curr_room = msg.room_number;
var room_data = io.sockets.adapter.rooms[curr_room];
var temp_room = [];
console.log(room_data);
for (var id in room_data) {
temp_room.push(io.sockets.adapter.nsp.connected[id]);
}
var player1_id = temp_room[0].id;
var player2_id = temp_room[1].id;
console.log(player1_id);
console.log(player2_id);
console.log(msg.player_id);
if (player1_id && player2_id) {
if (player1_id === msg.player_id) {
temp_room[1].emit('update coords', "dude");
} else if ("ffff" === msg.player_id) {
console.log('tessting to see wheter i can be in')
temp_room[0].emit('update coords', "sweet");
}
}
So this works without any errors and when I console.log room data I see two sockets which is what I want, however if I change "ffff" to player2_id I get this weird error where for the first iteration of this function I get the two sockets but after that the first player disconnects and I'm left with
var player1_id = temp_room[0].id;
^
TypeError: Cannot read property 'id' of undefined
I have absolutely no idea why having this check would make the user disconnect and furthermore I have no idea why the user is disconnecting if the page remains open.
Anybody have any insights, it would be greatly appreciated!
Thanks!!

Related

With twilio: Mute a specific participant in a room

I made a lot of searches, and tried many things. Let's take the same scenario from this question.
Bob, Alice, and Jane are in a conference. I want Bob (the hoster of the room) to be able to mute Alice so sounds from Alice will not come through the room, but Alice still hear sounds from all other participants (Bob & Jane)
Imagine the scenario when you are in conference, and someone disturbs the Live. So I want the hoster to be able to mute or remove the audio track of that person.
So far, I can mute the audio tracks for the local participant with:
$(document).on("click",'.mute-audio',function(){
if(room){
let microEnabled = true;
microEnabled = !microEnabled;
room.localParticipant.audioTracks.forEach(function(audioTrack) {
audioTrack.enable(microEnabled);
});
}
});
We simply iterate through audioTracks of the local participant, and if I want to do so for a specific User, I just need to know his identity and I am good to go:
$(document).on("click",'.mute-user',function(){
var identity = $(this).data('identity'); // i.e the unique ID USER2323
if(room){
var user_to_mute;
room.participants.audioTracks.forEach(function(participant) {
audioTrack.enable(microEnabled);
user_to_mute = participant;
// We retrieve the target
return;
});
let m_Enabled = true;
m_Enabled = !m_Enabled;
if(user_to_mute){
user_to_mute.audioTracks.forEach(function(audioTrack) {
audioTrack.enable(m_Enabled);
// the error is here I think
return;
});
}
}
});
But it doesn't work, when I try I got this error from the browser
TypeError: audioTrack.enable is not a function
It looks like some copy paste errors to me, but not being familiar with the API, I'm making a few assumptions:
$(document).on("click",'.mute-user',function(){
var identity = $(this).data('identity'); // i.e the unique ID USER2323
if(room){
var user_to_mute;
// in the next line, we remove .audioTracks, since it looks like
// you want to iterate over participants, and removed audioTrack.enable
// since it's not defined yet
room.participants.forEach(function(participant) {
if (identity == participant.identity) {
// We retrieve the target
user_to_mute = participant;
return;
}
});
let m_Enabled = true;
m_Enabled = !m_Enabled;
if(user_to_mute){
user_to_mute.audioTracks.forEach(function(audioTrack) {
audioTrack.enable(m_Enabled);
return;
});
}
}
});

Chrome App bluetooth

I am trying to create a chrome app that displays when a certain Bluetooth device is sending data. specifically, I have 2 bluetooth mice and I want to identify which one is being moved at a specific time.
I followed the Chrome dev doc and was successful until I tried to implement adding a listener on receive to view the data coming from the device. I am getting a "Cannot read property 'addListener' of undefined" error.
This is when I started getting this error:
Error message
Here's the code I'm working with
chrome.bluetooth.getDevices(function(devices) {
for (var i = 0; i < devices.length; i++) {
//Displaying device names
console.log(i+": "+devices[i].name);
}
//uuid for a specific device
var uuid = "00001200-0000-1000-8000-00805f9b34fb";
// var uuid = devices[4].uuid;
var onConnectedCallback = function() {
if (chrome.runtime.lastError) {
console.log("Connection failed: " + chrome.runtime.lastError.message);
} else {
// Profile implementation here.
}
};
chrome.bluetoothSocket.create(function(createInfo) {
chrome.bluetoothSocket.connect(createInfo.socketId,
devices[4].address, uuid, onConnectedCallback);
console.log(createInfo);
chrome.bluetoothSocket.onRecieve.addListener(function(receiveInfo) {
if (receiveInfo.socketId != socketId)
return;
console.log(receiveInfo);
});
});
});
Checked out this docs and managed to see a similar code snippet:
chrome.bluetoothSocket.onRecieve.addListener(function(receiveInfo) {
if (receiveInfo.socketId != socketId)
return;
// receiveInfo.data is an ArrayBuffer.
});
If you look at it carefully, it seems there was a typo in the sample at the onRecieve part. It should be onReceive. You can see a correct sample here.
I before E, except after C.

Chrome cast not receiving messages

I'm trying to send a message to my custom receiver for ChromeCast. I use the following code in my Android app to send a code to the receiver;
Cast.CastApi.sendMessage(mApiClient, "urn:x-cast:move", "TEST");
On the receiving side I have the following code;
window.mediaElement = document.getElementById('media');
window.mediaManager = new cast.receiver.MediaManager(window.mediaElement);
window.castReceiverManager = cast.receiver.CastReceiverManager.getInstance();
window.castReceiverManager.start();
window.castReceiverManager.onSenderConnected = function(event) {
//This is called
}
window.customMessageBus = window.castReceiverManager.getCastMessageBus('urn:x-cast:move', cast.receiver.CastMessageBus.MessageType.STRING);
var defaultFunction = window.customMessageBus.onMessage;
window.customMessageBus.onMessage = function(event) {
//This is not called
defaultFunction(event);
};
As I pointed out in the code, the 'onSenderConnected' is called, so I know it connected to the app. But when I try to send a message over the custom messagebus, it doesnt give me anything. I'm completely new to android and cast, so I could be doing a thousand things wrong. Can anybody help me solve this?
I solved it myself. What I did wrong was starting the castReceiverManager before adding the custom namespace. So the correct code for the receiver compared to what I posted in the question would be;
window.mediaElement = document.getElementById('media');
window.mediaManager = new cast.receiver.MediaManager(window.mediaElement);
window.castReceiverManager = cast.receiver.CastReceiverManager.getInstance();
//Removed the start here
window.castReceiverManager.onSenderConnected = function(event) {
//OnConnect
}
window.customMessageBus = window.castReceiverManager.getCastMessageBus('urn:x-cast:move', cast.receiver.CastMessageBus.MessageType.STRING);
var defaultFunction = window.customMessageBus.onMessage;
window.customMessageBus.onMessage = function(event) {
//OnMessage
defaultFunction(event);
};
//Start at the end
window.castReceiverManager.start();

Check for duplicate record in Chrome Storage extension before saving

I'm developing a small Chrome extension that would allow me to save some records to chrome.storage and then display them.
I've managed to make the set and get process work as I wanted (kinda), but now I'd like to add a duplicate check before saving any record, and I'm quite stuck trying to find a nice and clean solution.
That's what I came up for now:
var storage = chrome.storage.sync;
function saveRecord(record) {
var duplicate = false;
var recordName = record.name;
storage.get('records', function(data) {
var records = data.records;
console.log('im here');
for (var i = 0; i < records.length; i++) {
var Record = records[i];
if (Record.name === recordName) {
duplicate = true;
break;
} else {
console.log(record);
}
}
if (duplicate) {
console.log('this record is already there!');
} else {
arrayWithRecords.push(record);
storage.set({ bands: arrayWithRecords }, function() {
console.log('saved ' + record.name);
});
}
});
}
I'm basically iterating on the array containing the records and checking if the name property already exists. The problem is it breaks basic set and get functionality -- in fact, when saving it correctly logs 'im here' and the relative record object, but it doesn't set the value. Plus, after a while (generally after trying to list the bands with a basic storage.get function) it returns this error:
Error in response to storage.get: TypeError: Cannot read property
'name' of null
I'm guessing this is due to the async nature of the set and get and my incompetence working with it, but I can't get my head around it in order to find a better alternative. Ideas?
Thanks in advance.

Problem creating an email with an attachment in Javascript

I'm initiating an email create, by calling the code below, and adding an attachment to it.
I want the user to be able to type in the receipient, and modify the contents of the message, so I'm not sending it immediately.
Why do I get a RangeError the 2nd time the method is called?
(The first time it works correctly.)
function NewMailItem(p_recipient, p_subject, p_body, p_file, p_attachmentname)
{
try
{
var objO = new ActiveXObject('Outlook.Application');
var objNS = objO.GetNameSpace('MAPI');
var mItm = objO.CreateItem(0);
mItm.Display();
if (p_recipient.length > 0)
{
mItm.To = p_recipient;
}
mItm.Subject = p_subject;
if (p_file.length > 0)
{
var mAts = mItm.Attachments;
mAts.add(p_file, 1, p_body.length + 1, p_attachmentname);
}
mItm.Body = p_body;
mItm.GetInspector.WindowState = 2;
} catch(e)
{
alert('unable to create new mail item');
}
}
The error is occuring on the mAts.add line. So when it tries to attach the document, it fails.
Also the file name (p_file) is a http address to a image.
Won't work outside of IE, the user needs to have Outlook on the machine and an account configured on it. Are you sure you want to send an email this way?
I'm trying it with this little snippet, and it works flawlessly:
var objO = new ActiveXObject('Outlook.Application');
var mItm = objO.CreateItem(0);
var mAts = mItm.Attachments;
var p_file = [
"http://stackoverflow.com/content/img/vote-arrow-up.png",
"http://stackoverflow.com/content/img/vote-arrow-down.png"
];
for (var i = 0; i < p_file.length; i++) {
mAts.add(p_file[i]);
}
Note that I left off all optional arguments to Attachments.Add(). The method defaults to adding the attachments at the end, which is what you seem to want anyway.
Can you try this standalone snippet? If it works for you, please do a step-by-step reduction of your code towards this absolute minimum, and you will find what causes the error.
first do mItm.display()
then write mItm.GetInspector.WindowState = 2;
this will work

Categories