Signalr specifies the Websocket Method for cross-Origin data transmission, signalrwebsocket
Cross-origin is commonly understood as the web service address behind two domain names, that is, they are all independent websites. In actual business situations, there may be many situations where cross-Origin data push is required,
For example, after the background of a ELE. Me merchant receives a confirmation order from the client, the background service pushes an order message to the merchant's foreground.
Signalr cross-origin code:
Public partial class Startup {public void Configuration (IAppBuilder app) {// For details about how to configure an application, visit http://go.microsoft.com/fwlink/?LinkID=316888 Try {Air. log. logger. default. trace ("Configure signalr"); // added the GlobalHost module of the MPs queue Exception Handling Module. hubPipeline. addModule (new ErrorHandlingPipelineModule (); GlobalHost. hubPipeline. addModule (new LoggingPipelineModule (); app. map ("/WebApiSignalr", map => {// supports cross-origin map. useCors (CorsOptions. allowAll); var hubConfiguration = new HubConfiguration {// You can enable JSONP by uncommenting line below. // JSONP requests are insecure but some older browsers (and some // versions of IE) require JSONP to work cross domain // EnableJSONP = true, EnableDetailedErrors = true }; map. runSignalR (hubConfiguration) ;}); Air. log. logger. default. trace ("signalr configured");} catch (Exception ea) {Air. log. logger. default. error ("Startup exception", ea );}}}
Front-end js Code:
1/*** 2 get the background Signalr service address and bind it to $. connection. notifyHub 3 */4 function bindNotifyServier () {5 var url = ""; 6 mwc. restApi. post ({7 // request address 8 url: '/Home/getpolicyurl', 9 // whether to lock UI10 isBlockUI: true, 11 // success function 12 success: function (data) {13 console. log ("Get push Message Address:" + data. notifyUrl); 14 url = data. notifyUrl; 15 initData (url); 16} 17}); 18} 19 20/** initialize data 21 */22 function initData (notifyUrl) {23 // obtain the message hub object 24 25 $. connection. hub. url = yyurl; 26 var notifyHubProxy = $. connection. notifyHub; 27 notifyHubProxy. client. required y = function (required y) {28 console. log ('Received message: '+ Y); 29 $ notfiy = JSON. parse (notify); 30 speckText ($ notfiy. message); 31 if (typeof (sort y. length )! = 'Undefined') {32 vm. notifies. push ($ notfiy); 33 console. log ('Press message'); 34 return; 35} 36 var hasExist = false; 37 // whether the message exists 38 $. each (vm. notifies, function (I, v) {39 if (v. id = policy. id) {40 vm. notifies. splice (I, 1, $ notfiy) 41 hasExist = true; 42 return; 43} 44}); 45 // Add 46 if (! HasExist) {47 vm. notifies. push ($ notfiy); 48 if (vm. notifies. length> 8) 49 vm. notifies. splice (7, 1) 50} 51 console. debug (vm. notifies); 52}; 53 $. connection. hub. start ({transport: ['webbuckets', 'longcollect']}). done (function () {54 console. debug ('Connection to the server successful! '); 55}). fail (function () {console. log ('Connection failed! ') ;}); 56 };
View Code
Background push code:
1 var notifier = policymanager. Current. Notifier; 2 vmsitenoworkflow vm1_y = new vmsitenoworkflow ();
3 vm1_y. Message = "test! "; 4 vmbench Y. Title =" test push "; 5 notifier. policyto (vmbench y, user. RoleId. ToString ());
Example:
For example, if the communication mode is websocket, check whether the communication mode is websocket. Then, check whether there is information in the circle under the red box under the Requet Headers of the http request.
Pay attention to the above js code. I wrote this section: $. connection. hub. start ({transport: ['webbuckets', 'longpolling ']})
This section means that the communication method of websocket is preferred. Otherwise, I feel that my actual environment supports websocket. In fact, the debugging result of the browser tells me that it is also another communication method.
Therefore, it is best to clarify the communication mode at start. The above js means that the priority is websocket and then long polling.