Display Interface
Open three such interfaces, and then upload the sending information to other interfaces to obtain the data at the same time.
1. Upload signalr jquery. signalr. js and reference it.
2. Create an MVC project.
3. Add the zookeeper category to global. asax, and add the zookeeper category to the category.
Protected void application_start ()
{
Routetable. routes. mapconnection <myconnection> ("Echo", "Echo/{* operation }");
}
4. Create myconnection and accept persistentconnection
In this example, clientid is the user ID information for sending data. It can be specified for a specific user or a group of users.
Protected override task onreceivedasync (string clientid, string data)
{
// Broadcast data to all clients
// Perform data processing here
Data = string. Format ("data volume: {0} Time: {1}", Data, datetime. Now. tostring ());
Return connection. Broadcast (data );
}
The token generation is as follows:
View code
Public class myconnection: persistentconnection { protected override task onreceivedasync ( string clientid, string data) { /// broadcast data to all clients DATA = string . format ( " the data volume is: {0} Time: {1} " , Data, datetime. now. tostring (); return connection. broadcast (data) ;}
5. Use the index. cshtml labels
View code
<SCRIPT type = " Text/JavaScript " > $ (Function (){ VaR Connection = $. Connection ( ' Echo ' ); Connection. Received (function (data) {$ ( ' # Messages ' ). Append (' <Li> ' + Data + ' </LI> ' ) ;}); Connection. Start (); $ ( " # Broadcast " ). Click (function () {connection. Send ($ ( ' # Msg ' ). Val ());});}); </SCRIPT>
The interface proxy is as follows:
View code
@ {Viewbag. Title = " Home Page " ;} <SCRIPT type = " Text/JavaScript " > $ (Function (){ VaR Connection = $. Connection ( ' Echo ' ); Connection. Received (function (data) {$ ( ' # Messages ' ). Append ( ' <Li> ' + Data + ' </LI> ' ) ;}); Connection. Start (); $ ( " # Broadcast " ). Click (function () {connection. Send ($ ( ' # Msg ' ). Val () ;}); $ ( " # Btnstop " ). Click (function () {connection. Stop ();});}); </SCRIPT> <H2> @ viewbag. Message </H2> <input type = " Text " Id = " MSG " /> <Input type = " Button " Id = " Broadcast " Value = " Sending " /> <Input type = " Button " Id = " Btnstop " Value = " Stop " /> <Ul id = " Messages " > </Ul>
Start and Stop.
VaR connection = $. Connection ('echo '); obtain response
Invalid Response Data:
Connection. Received (function (data ){
$ ('# Messages'). append ('<li>' + Data + '</LI> ');
});
Start connection: connection. Start ();
Stop disconnect: connection. Stop ();
Under the source region:Http://files.cnblogs.com/Teco/MvcApplicationSignalR.rar
The above-mentioned Token Generation: I canceled return connection. Broadcast (data );
Only send to yourself. If you want to send all, useReturn connection. Broadcast (data );