I'm building a chat app.But on logout button I can change bit value in database from 1 if user login to 0. But if user close the app or exit the browser without hitting logout, how can I change the bit value to 0?
Need answer programmatically.
When you close your browser, your webserver won't receive any feedback. Most browsers also do not allow you to implement javascript that does this. The only way to track this is by using something that polls your server periodically. (As #scgough put in the comments, SignalR is one of these technologies that you can use to do this.)
The downside of these techniques is that web browsers will generate web traffic to your webserver periodically to send 'heartbeat' signals, which in turn keeps the session alive for as long as the browser is open. This means this will consume a bit more web traffic and server resources.
Related
I am creating a new application in asp.net 4.0 which contains an online test module, and the application is supposed to handle 10-20k students at a time. Now my confusion is what should i use here, which should be:
Secure , I don't want kids to turn off javascript in browser and get unlimited time for test.
Which should not overhead the network traffic.
Which have a pause/start function with try limits.
Should fire the needed function on server side after time is complete (e.g. redirect to another page via c# function)
Any help and example code is appreciated.
Regards
Alok Sharma
FYI: I am not experienced with jquery.
If the security of timer is critical you should not fully rely on javascript code. I would recommend to keep the time when test is started for a specific user on both sides, server and client. Then implement javascript timer which periodically synchronizes with web server. When user completes the test and sends results to the server you should check if the timeout is elapsed on server side.
In additional I would add a logic in order to ban the possibility to open the page and start test without javascript enabled.
What I am trying to do with SignalR:
1- Setting the hidden field from a session UserID on page load and send this ID back to server's SignalR Hub to start the polling thread for given user.
2- Terminate the thread when user leaves the site.
Right now I am doing AJAX requests to server every 30 secs per user to check for new user messages.I Just want to replace it with SignalR. I am able to create the user level thread in a HUB when the users session is created by setting the hidden field on page load and then setting the session variable via ajax request so the new thread is not created for the same user again and again e.g page refresh. The thread is periodically checks after (15 sec) for newly arrived messages. The main issue is how do I terminate the thread created for a specific user when its session ends. Is this the right way to use SignalR ?
This doesn't sound like good design - polling is never good, not for this kind of problems anyway. It would be better if you could deliver the message instantly. Are you using multiple webservers? If not, then deliver the messages locally inside the application. If you are using multiple webservers, consider using something like RabbitMQ, Redis or similar to send messages between the servers.
How to refresh aspx page from sql server.I am using asp.net,C-Sharp with SQL Server 2008.What i mean is i have table, say Table1.If any DML operation is performed (Update,Insert etc) to Table1,then my page,say Page1.aspx should autmatically get refreshed.I can't use timer for refreshing the page.I need to trigger the refresh from database.
Even though the server may be notified when data has changed, the real challenge is communicating those changes to the client in real-time without requiring a timer or user interaction.
You have a couple of options:
Your best bet is to use a WebSocket, which enables bidirectional communication between the client and server. This is the solution I would pick.
Here are some examples using WebSockets:
Building real-time web apps with WebSockets using IIS, ASP.NET and WCF
HTML5 C# WebSockets Server and ASP.NET Client Implementation
C# WebSocket Server
WebSockets in ASP.NET 4.5
WebHooks and WebSockets in ASP.NET
There are a few good libraries around too that will take care of most of the leg work. A couple to check out are WebSync and PokeIn. Both products offer decent documentation and community editions that you can download for free.
Here are some tutorials to check out:
WebSync Tutorials
PokeIn Basic Tutorial / PokeIn Advanced Tutorial
Use AJAX to poll for changes every X number of seconds. If changes are detected reload the page, otherwise do nothing.
You probably want to look into the SqlDependency object. This object will notify you of changes to a specified database query in real time. When your application receives a message from the database, you can simply refresh the page in your code-behind.
I wonder if you could adapt SignalR to send a message to the client to prompt a refresh?
This tutorial could get you started.
I wonder if the following is possible:
I have a dll that I have referenced in my web site. This dll makes a remote socket connection. The socket connection is waiting in the background and the dll reports back data through events after the socket has received some data.
The connection is opened during load of page.
Now, I would, for example, like to update a label on the page when new data has arrived.
I am not sure how this would work. I assume that I could set some kind of timer on page that updates a control but it does not seem "optimal" as I already call code behind through my events. "Optimally" the UpdatePanel or whatever updates the interface would wait for events and "update" when events has occurred and not based on time.
My question is - is this possible?
You can use techniques called "long pulling" or "web sockets".
There are libraries, like SignalR that can help you.
They generally use web sockets when client's browser supports and long pulling when not.
Using these libraries you can "push" commands/data from server to client's browser, just as you want.
Server cannot push the client, this is the rule how web works.
There are two possible ways to complete your task.
Put timer on the page and make requests on server too see, if something changed
Open long pulling connection between client and server, like facebook does. That listens
to the events and gets data from the server. It can be, for example xmpp or any other.
You should think about turning this around; rather than initiate the call from the code behind asynchronously and then update the client, you should deliver your original page to the client.
Once the page has been loaded client side, you can make an AJAX call to the server to retrieve the data you want - and display a little "I'm loading" symbol while this happens.
Page Methods are ideal for this.
The classic ASP.Net web page is supposed to last for the duration of the users request. When a postback happens, it effectively rebuilds the state of the call from the user's viewstate, session and any other state you've maintained. It never cares if the user goes away, for example (although it might feel a bit lonely).
Having it hang around for longer is problematic in a number of ways, however, you can implement client callbacks. However, although the server initiates this, the client manages the lifecycle, so it's analogous to page methods.
I would like to flush a user's session with my website as soon as they close their web browser. How do I go about implementing that?
This is not easy as http is stateless. So you don't have an open connection which is closed when the browser closes. No way is guaranteed to work. A connection could be dropped, or power go down which would make any 'onClose' event unreliable.
You can time out sessions after a certain amount of inactivity. However there is no guarantee that a user hasn't just gone to lunch and wants an open session when they return.
If you are worried about indicating people as offline you could do some polling in ajax to keep sessions alive and destroy sessions that have not been used in longer than the poll interval. But if you are just worried about resources I would guess that this will do more harm than good.
Make an ajax call at onbeforeunload() event and call :
Session.Abandon();
You have no event on server-side triggered when user closes window or leaves the page, you can use the timeout of sessions the check it, and then, ensure the session does't dies on an idle user, that could be at the bathroom, or making coffe, validating it with AJAX or so.
MakeDummyRequest(); // with a simple http request, you can keep your session alive.
As other indicated your best bet is probally to make an Ajax call which will keep the session alive. This call can occur at fast interval if you keep it lean. Then you can use some sort of session timeout algorithim.
With that said there a couple of things to note. You will probally want to build a way in the protocol to force a user to logout of the application. Imagine that a user is logged in to your application and he goes home for the weekend without shutting down his computer.
Over the weekend you do a deploy which will invalidate all the ajax communication becuase you modified the protocol or something. Unless you force him out he will continue to be logged in and could be getting errors when he comes in Monday morning.