The ASP. NET SIGNALR is a class library that Microsoft implements for real-time communication. In general, SIGNALR uses JavaScript's long polling to implement client and server communication, and SIGNALR also supports websockets communication as websockets occurs in HTML5. In addition, the program developed by SIGNALR is not limited to hosting in IIS, but can be hosted in any application, including consoles, client programs and Windows services, and also supports mono, which means it can be deployed across platforms in a Linux environment.
There are two types of objects inside SIGNALR:
- HTTP Persistent connection (Persisten Connection) object: Used to resolve long-time connections. It is also possible for the client to proactively request data to the server, and the server side does not need to implement too much detail, only to handle the five events provided in Persistentconnection: onconnected, onreconnected, onreceived, OnError and OnDisconnect.
- Hub (Hub) object: Used to solve the real-time (realtime) Information exchange function, the server can use the URL to register one or more hubs, as long as the connection to the hub, you can share with all the client to send information to the servers, while the service side can invoke the client's script.
SIGNALR the entire information exchange package, the client and the server are using JSON to communicate, all the hub information declared on the service side, will generate JavaScript output to the client. NET relies on proxies to generate proxy objects, and the inside of proxies is to convert JSON into objects.
A message reminder is that when a customer has a new message, a pop-up box is alerted at the bottom right corner of the client. The idea to implement this function is:
- The SIGNALR server push message to the client is implemented by calling the client's ReceiveMessage method to attach the message to the chat record, so we can implement the frame logic in the client's ReceiveMessage method.
- After finding a way to define the location, it is natural to find a better frame effect JS class Library, where the use of inotify library to achieve. The GitHub address for this library is: https://github.com/jaywcjlove/iNotify, online test address: http://jslite.io/iNotify/
- You see QQ or the message reminds, the message is usually not in the chat of the current tab page will pop up, we can use the HTML5 Visibilitychange event to achieve, but I here is through the loss of focus, that is, the focus event.
Javascript
//Receiving MessagesSystemHub.client.receivePrivateMessage =function(Fromuserid, userName, message) {//the code in topic two //code for message Reminders if(Active = =false) { varin =NewiNotify ({effect:' Flash ', Interval:500, Audio: {file: ['/music/msg.mp3 ']}, notification: {title:Notification ", Body:' You have a new message ' } }); In.settitle (true). Player (); In.setfavicon (true). Settitle (true). Notify (); } }; }
Using SIGNALR to implement message reminders