I have got a small project working with SignalR, however i am getting some very inconsistent behavior.
<script type="text/javascript">
$(function () {
var chat = $.connection.brewBattleHub;
$.connection.hub.start().done(function () {
$("#broadcast").click(function () {
// Call the chat method on the server
chat.server.roll($("#username").val(), $("#drinkname").val());
});
chat.server.sendMessage("SignalR loaded...");
});
});
</script>
When i load the page, sometimes i am seeing the message "SignalR loaded", other times i am not.
There's is some other functionality on the page also, and sometimes this does not work either. If i click buttons and make things happen enough it will eventually all come through in one go... from this point it is all golden and works perfectly.
does start().done()? not ensure it is all ready?
===
addendum, i am not referencing jquery mobile (google mentioned there is a bug when doing so)
I had a similar issue this week except that the code in .done() never ran. I enabled logging and nothing was ever logged. Checking the browser's console there were no errors. Looking in the browser's dev tools I could see that there was no network activity when I called start(). I confirmed that the proxy returned by $.connection.myhubclass had all my methods on it so I knew it could talk to the server and that the server side code was set up right.
After reviewing the code and documentation over and over again I was convinced that I wasn't doing anything wrong. SignalR is so simple it's pretty tough to do it wrong if you follow the examples. I beat my head on the desk for quite a long time trying to figure this out.
Then I noticed that if I ran SignalR's start method from the console it would work. That led me to believe that it was trying to start the connection too early. I solved my problems by changing the initialization code from this:
$.connection.hub.start().done(function () {
//Do interesting stuff
});
To this:
setTimeout(function () {
$.connection.hub.start().done(function () {
//Do interesting stuff
});
}, 5000);
I'm sure I can reduce that delay from 5 seconds to something shorter but the extra time appeared to be what was needed to let the browser get itself ready to create the connection. Once that delay was in there the logging started working, the connection came up, the server side OnConnected() ran and done() was run on the client.
After figuring this out I double checked the order of my javascript loading, thinking maybe I was loading something out of order but the order is right according to the SignalR samples. I load jQuery, SignalR, /signalr/hubs, then my script file that initializes everything.
I am open to suggestions as to why that delay was needed. I don't see it in any of the docs so I know it could be something I did. Fortunately for this page a minor delay before starting up SignalR is not a problem.
Try enabling SignalR logging to help debug your issue. You might also want to add a fail handler in case start isn't succeeding (though this is unlikely). Once you have done this you can look through your browser's F12 tools to look at the JS and network logs.
<script type="text/javascript">
$(function () {
$.connection.hub.logging = true;
var chat = $.connection.brewBattleHub;
$.connection.hub.start().done(function () {
$("#broadcast").click(function () {
// Call the chat method on the server
chat.server.roll($("#username").val(), $("#drinkname").val());
});
chat.server.sendMessage("SignalR loaded...");
}).fail(function (reason) {
console.log("SignalR connection failed: " + reason);
});
});
</script>
I've found a solution (as far by my problem).
I had SignalR loading for some 7-8 seconds on IE11, I've tried to "debug" it in console and I've found the cause of the loading delay:
[13:13:04 GMT+0200 (...)] SignalR: Binding to iframe's load event.
[13:13:09 GMT+0200 (...)] SignalR: foreverFrame timed out when trying to connect.
[13:13:09 GMT+0200 (...)] SignalR: Stopping forever frame.
It took 5 sec. to try to load iframe.
The neat solution is to turn the iframe off, as IE11 can normally use longPolling:
$.connection.hub.start(({ transport: ['webSockets', 'serverSentEvents', 'longPolling'] }))
I had a similar issue that only appeared when trying to reestablish the connection after disconnect. I called start() from the disconnect handler and then tried to send a message from start.done(). Then I got an error message that SignalR was not ready.
I noticed in the log that start.done() resolved immediately so there was in my case a call from disconnect to start and then to send. I added a setTimeout in the disconnect handler so that start and send were decoupled from disconnect. That resolved the issue for me.
Related
I'm having incredible difficulty setting up the Twilio Client in Meteor JS, and would really appreciate any help.
I have extracted the relevant code and error logs below. So far as I can tell, it should be simple. The code is just grabbing an authtoken which I have previously generated, and then trying to set up the device using that authtoken. But it's not working.
'click #initializeDevice'(event) {
var thisAuthToken = Session.get('myAuthToken');
console.log(thisAuthToken); // I have confirmed with Twilio support that these authtokens are correctly generated
const Device = require('twilio-client').Device;
Device.setup(thisAuthToken, { debug: true });
var myStatus = Device.status()
console.log(myStatus); //this is logging "offline"
Device.on('ready',function (device) {
log('Twilio.Device Ready!'); //this is not logging anything
});
},
When that code runs, it generates the following logs:
eyJhbGciDpvdXRnb2luZz9hcHBTaWQ9QVA2NDE2MzJmMzA1ZjJiY2I[Note:I have deleted part of the middle of the logged authtoken for the purpose of this public post]5YmMxOGQyOWVlNGU2ZGM0NjdmMzRiNDVhNCIsImV4cCI6MTU3Nz0ygbJKTx15GgNCWDkm-iUPjn_O1NZU6yovp4vjE
modules.js?hash=69069bec9aeba9503ae3467590cf182be57d9e62:3605 Setting up VSP
modules.js?hash=69069bec9aeba9503ae3467590cf182be57d9e62:3605 WSTransport.open() called...
modules.js?hash=69069bec9aeba9503ae3467590cf182be57d9e62:3605 Attempting to connect...
modules.js?hash=69069bec9aeba9503ae3467590cf182be57d9e62:3605 Closing and cleaning up WebSocket...
modules.js?hash=69069bec9aeba9503ae3467590cf182be57d9e62:3605 No WebSocket to clean up.
modules.js?hash=69069bec9aeba9503ae3467590cf182be57d9e62:3605 Could not connect to endpoint: ws does not work in the browser. Browser clients must use the native WebSocket object
modules.js?hash=69069bec9aeba9503ae3467590cf182be57d9e62:3605 Closing and cleaning up WebSocket...
modules.js?hash=69069bec9aeba9503ae3467590cf182be57d9e62:3605 No WebSocket to clean up.
calltemplate.js:31 offline
I'm doing this all from a local server, tunneled through NGROK. I've also set up the Twilio back end, linked the app, purchased a number, etc.
So far as I can tell, the issue, from the logs, appears to be something to do with the way that Meteor uses WebSockets.
Could not connect to endpoint: ws does not work in the browser. Browser clients must use the native WebSocket object
This is a not a Meteor related problem rather than browser issue.
Make sure your browser supports WebRTC
BTW, Your browser might be supporting it but you'd need to enable it.
I'm using SignalR 2.2.2 to send users messages from my backend. When a user is logged in, and if other conditions are met, their connection is added to a group with the user's userId on my message hub.
It works great, as long as they have ~10 or fewer tabs/windows open. Any beyond that, they're stuck in "Loading..." indefinitely.
It seems to just be getting stuck on $.connection.hub.start();
I don't necessarily need to allow each user an infinite amount of signalr connections, but breaking the entire site for them on 10 open tabs is a problem.
OKAY BUT HERE'S THE THING. When I change my project's server setting from Local IIS to IIS Express, this problem vanishes! BUT! When we build the solution & put it on the test server, it's still broken.
What is going on???
I've tried catching or handling an error, but it still just hangs there.
$(function () {
if (loggedInUser != null)
{
var user = loggedInUser.UserId;
var messaging = $.connection.messageHub;
if (conditions) {
$.connection.hub.start().done(function () {
messaging.server.joinGroup(user);
});
}
}
});
I can get this to work with IIS Express. Now I need it to work like that on my test (and then later production) Server.
What else can I even try?
I am looking to create a websocket on Meteor Server (not client) to connect to an external site. I know the URL I am going to be hitting as well as what data to expect, but I am unclear as to how exactly to create the websocket itself. All the searching I do presents me with solutions for the client, but I have yet to run into anything that serves as a server solution.
Is there anything out there I missed that fills this purpose? Atmosherejs.com doesn't list anything, and searching around on google/github didn't reveal anything either. Is there something built into Meteor that already accomplishes this?
The following code is for opening a Socket in Meteor on Port 3003. It convert the data from the socket (sendet from client) to a JSON-Object. So this means, the following code is a socket, which receive JSON.
Fiber = Npm.require('fibers')
// server
Npm.require('net').createServer(function (socket) {
console.log("connected");
socket.on('data', function (data) {
socket.write("hello!");
var o = JSON.parse(data.toString());
console.log(o);
Fiber(function() {
console.log('Meteor code is executing');
//=> Meteor code
}).run();
//console.log(data.toString());
//socket.close();
});
})
.listen(3003);
I'm seeing some odd/unexpected behaviour using the SignalR 2.0.3 libraries.
I'm trying to hook some call backs in the client and they don't seem to be getting fired.
I'd really like to get the reconnecting, reconnected and disconnected call backs working in JS - client side.
SignalR does connect successfully and I've got communication between the server and client.
Here are a couple of code snippets:
$.connection.reconnecting = function () {
alert("reconnecting");
};
$.connection.reconnected = function () {
alert("We have been reconnected");
};
$.connection.disconnected = function () {
alert("We are disconnected!");
};
I could have sworn that I had these firing when I was using SignalR 1.x.
Currently, we using version 2.0.3.
I'm running a 2 machine setup where my server app (and hub) is on 1 machine.
Here are the steps I'm using to reproduce:
1) Connect from a second machine. I looked at the network traffic in the browser
console and it looks ok.
2) Next I disconnect the client machine from the network. In the console I see SignalR
attempting to reconnect. (This is as expected)
3) Ultimately after N retries, it looks like it disconnects. (I see the final retry is "Canceled"
in the console output.
4) However, my disconnected() handler does not get called.
Any insights or thoughts how to track this down?
Thanks,
JohnB
With SignalR 2.x, you can register your callbacks like this:
$.connection.hub.reconnecting(function() {
alert("reconnecting");
});
$.connection.hub.reconnected(function() {
alert("We have been reconnected");
});
$.connection.hub.disconnected(function() {
alert("We are disconnected!");
});
You can also add callbacks using $.connection.hub.stateChanged(). See documentation.
I'm using SignalR but have run into a problem. When a connection is started in one browser window and then a user logs in in another browser window the User identity is changed (this causes the error 'System.InvalidOperationException: Unrecognized user identity. The user identity cannot change during an active SignalR connection' on the server when a method is called on the hub.
I'm using this code on the client:
proxy.server.analyze(content)
.done(function () {
console.log('Success!');
})
.always(function () {
console.log('This is always called!');
})
.fail(function (error) {
console.log('This is never called!');
});
When I'm seeing errors on the server the fail function is never being called so there appears to be no way on the client to handle this problem and stop and start the connection.
So is there a "best practice" way of handling this case? How can I detect on the client that the user identity has changed in another browser window and stop and re-start the connection?
This is a known issue.
It is fixed in the next release. Here's the issue that ended up also fixing your issue: https://github.com/SignalR/SignalR/issues/2106.
Lastly, in the next release (0.2.0) what will happen is the connection will throw an error and stop itself. Therefore you'll be able to handle your case via either the error handler or you can of course you can tie into the "disconnected" event.
If you're willing to try a pre-releases you can always pull from the offical source or webstack nightly (http://www.myget.org/F/aspnetwebstacknightly/)