Reference article: http://www.cnblogs.com/nywd/p/3691813.html
In the previous section, has been implemented, the current intra-domain communication, this section, the introduction of cross-domain instant communication, since to do, we must make this push and chat server as a separate server to facilitate the expansion of use, so that the use of cross-domain technology, since AJAX-based, then cross-domain is certainly based on JSONP, Let's look at the basic configuration of cross-domain:
1, the configuration of the server, we open the project Global.asax, in the Application_Start to do the following configuration:
protected void Application_Start () { varnew hubconfiguration (); true ; RouteTable.Routes.MapHubs (config); Arearegistration.registerallareas (); Webapiconfig.register (globalconfiguration.configuration); Filterconfig.registerglobalfilters (globalfilters.filters); Routeconfig.registerroutes (routetable.routes); }
Config. Enablecrossdomain = true;
This code specifies that all current hubs can be used across domains.
2, web-side configuration, we create a new project, and then add an HTML page, msg.html, add the header section:
<script src= "/scripts/jquery-1.6.4.min.js" type= "Text/javascript" ></script><script src= "/Scripts/ Json2.js "type=" Text/javascript "></script><script src="/scripts/jquery.signalr-1.0.1.min.js "type=" Text/javascript "></script><script src=" http://localhost:2154/signalr/hubs "type=" Text/javascript " ></script>
Http://localhost:2154/signalr/hubs is the address of our push server, if really online, definitely use the domain name of the push server, such as: push.xxx.com, and then we write the JS method, the HTML part basically consistent:
<script type= "Text/javascript" > $(function() {$.connection.hub.url= "HTTP://LOCALHOST:2154/SIGNALR"; //Proxy created on the fly varChat =$.connection.pushhub; //Declare a function on the chat hub so the server can invoke itChat.client.addMessage =function(message) {writeEvent (<b>ny</b> said to everyone: ' + message, ' Event-message '); }; $("#broadcast"). Click (function () { //Call the Chat method on the serverChat.server.send ($ (' #msg '). Val ()). Done (function() {Console.log (' Sent message success! '); }). Fail (function(e) {Console.warn (e); }); }); //Start the connection$.connection.hub.start ({xdomain:true}); //A function to write events to the page functionwriteEvent (EventLog, logclass) {varnow =NewDate (); varNowstr = now.gethours () + ': ' + now.getminutes () + ': ' +now.getseconds (); $(' #messages '). Prepend (' <li class= ' + logclass + ' "><b> ' + nowstr + ' </b> ' + eventLog + ' .</li> ')); } });</script>
1, first to specify the hub root address: $.connection.hub.url = "HTTP://LOCALHOST:2154/SIGNALR";
2. When you start the connection, add the cross-domain parameter: $.connection.hub.start ({xdomain:true});
Very simple, now that the configuration is complete, let's start the browser test:
As you can see, 2 Web servers have been interoperable, and we specify the use of IE7, the compatibility is very good, XP is eliminated IE6 we also contingency:).
What we are going to do, find a UI designer, the chat window of our site to beautify, do an iframe, in the lower right corner, click, both appear in our chat interface, and then other client users and our customer service staff to chat, is not great,
We're going to have dinner tomorrow. We continue to introduce client connection SIGNALR to enable merchant clients and users to communicate directly.
SignalR Cross-domain settings