AJax¼¼Êõ

jQueryÖ®EasyUI¿ª·¢¼¼ÇÉ»ã×Ü(3)

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2018-02-10 17:06 ÎÒÒªÆÀÂÛ( )

var connection = $.hubConnection(localhost:23123);//Èç¹ûÇ°ºó¶ËΪͬһ¸ö¶Ë¿Ú£¬¿É²»Ìî²ÎÊý¡£Èç¹ûÇ°ºó¶Ë·ÖÀ룬ÕâÀï²ÎÊýΪ·þÎñÆ÷¶ËµÄURLvar demoChatHubProxy = connection.createHubProxy(demoChatHub);demoChatHu


var connection = $.hubConnection('localhost:23123');//Èç¹ûÇ°ºó¶ËΪͬһ¸ö¶Ë¿Ú£¬¿É²»Ìî²ÎÊý¡£Èç¹ûÇ°ºó¶Ë·ÖÀ룬ÕâÀï²ÎÊýΪ·þÎñÆ÷¶ËµÄURL var demoChatHubProxy = connection.createHubProxy('demoChatHub'); demoChatHubProxy.on('addMessageToList', function(userName, message) { console.log(userName + ' ' + message); }); connection.start() .done(function(){ console.log('Now connected, connection ID=' + connection.id); }) .fail(function(){ console.log('Could not connect'); });

ÐèҪעÒâµÄÊÇ£¬¿ªÊ¼Á¬½Ó֮ǰ£¨µ÷Óà start ·½·¨Ö®Ç°£©£¬×îºÃ×¢²áÖÁÉÙÒ»¸öʼþ´¦Àí·½·¨£¬Èç¹ûûÓÐ×¢²áµÄ»°£¬HubsµÄ OnConnected ·½·¨½«²»»á±»µ÷Óã¬ÄÇô¿Í»§¶ËµÄ·½·¨¾Í²»Äܱ»·þÎñ¶Ëµ÷Óã¨ÕâÈÝÒ×Âñ¿Ó£¬ËùÒÔÒªÌáÇ°×¢²á·½·¨£©¡£

2.¿Í»§¶ËÈçºÎµ÷Ó÷þÎñÆ÷¶Ë·½·¨
ʹÓà invoke£¬×¢Òâµ÷Ó÷þÎñÆ÷¶ËµÄ·½·¨ÃûÊ××Öĸ¿ÉÒÔ²»´óд£¬Èç¹û·½·¨Ãû³ÆÒªÏÞÖƱØÐë´óд£¬ÐèÒªºó¶Ë×öÅäÖá£


demoChatHubProxy.invoke('newChatMessage', {name:'a',message:'b'});

3. ·þÎñÆ÷¶Ëµ÷Óÿͻ§¶Ë·½·¨

Ê×ÏÈ¿Í»§¶ËҪע²á·½·¨²ÅÄÜÈ÷þÎñÆ÷¶Ëµ÷Óã¬Ê¹Óà on ·½·¨×¢²á¡£


demoChatHubProxy.on('addMessageToList', function(userName, message) { console.log(userName + ' ' + message); });

4 ÔÚVueÏîÄ¿ÖÐʹÓÃSignalR

Ê×ÏÈ°²×° SignalR µÄpackage£¬ÐèҪעÒâµÄÊÇ SignalR ÒÀÀµ jQuery¡£

npm i signalr,jquery

ΪÁË·½±ã£¬ÔÚwebpack.base.conf.jsÖÐ×¢²áÈ«¾ÖµÄjQuery


plugins: [new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery', 'window.jQuery': 'jquery', 'root.jQuery': 'jquery' }) ]

È»ºóÔÚmain.jsÖÐÒýÈë SignalR

import 'signalr'

Õâʱºò¾Í¿ÉÒÔÔÚVueÏîÄ¿ÖÐʹÓÃSignalRÁË£¬ºó¶ËµÄÏà¹ØÅäÖÃÔÝʱÂÔ¹ý¡£

н¨Ò»¸ösignalr.js


import { Message } from 'element-ui'; const HUBNAME = 'DefaultHub'; /*¿Í»§¶Ëµ÷Ó÷þÎñÆ÷¶Ë·½·¨*/ //¸üж©µ¥´òÓ¡´ÎÊý const updateOrderPrint = { name:'updateOrderPrint', method:function(data){ console.log(data) } } /*·þÎñÆ÷µ÷Óÿͻ§¶Ë·½·¨*/ // ´òӡж©µ¥ const printNewOrder = { name:'printNewOrder', method:function(data){ console.log(data) } } const get = { name:'Get', method:function(data){ console.log(data) } } //·þÎñÆ÷¶ËµÄ·½·¨ const serverMethodSets = [updateOrderPrint]; //¿Í»§¶ËµÄ·½·¨ const clientMethodSets = [printNewOrder,get]; //½«ÐèҪע²áµÄ·½·¨·Å½ø¼¯ºÏ // ½¨Á¢Á¬½Ó export function startConnection() { let hub = $.hubConnection(process.env.HUB_API) let proxy = createHubProxy(hub) //ÐèÒªÏÈ×¢²á·½·¨ÔÙÁ¬½Ó hub.start().done((connection) =>{ console.log('Now connected, connection ID=' + connection.id) }).fail(()=>{ Message('Á¬½Óʧ°Ü' + error); console.log('Could not connect'); }) hub.error(function (error) { Message('SignalR error: ' + error); console.log('SignalR error: ' + error) }) hub.connectionSlow(function () { console.log('We are currently experiencing difficulties with the connection.') }); hub.disconnected(function () { console.log('disconnected') }); return proxy } // ÊÖ¶¯´´½¨proxy export function createHubProxy(hub){ let proxy = hub.createHubProxy(HUBNAME) // ×¢²á¿Í»§¶Ë·½·¨ clientMethodSets.map((item)=>{ proxy.on(item.name,item.method) }) return proxy }

ÕâÑù£¬ÔÚ×é¼þÒýÈësignalr.jsºóµ÷ÓÃstartConnection·½·¨¼´¿É½¨Á¢Á¬½Ó¡£

Ïà¹ØÍƼö£º

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • AjaxÇëÇó±¨´í ¡°CAUTION: Provisional headers are shown¡±

    AjaxÇëÇó±¨´í ¡°CAUTION: Provisional headers are shown¡±

    2018-02-10 16:04

  • jquery+ajax+ashx

    jquery+ajax+ashx

    2018-01-26 16:04

  • jQueryAjaxͬ²½Òì²½Çø±ð

    jQueryAjaxͬ²½Òì²½Çø±ð

    2018-01-25 16:02

  • jQuery+ajax¶ÁÈ¡²¢½âÎöXMLÎļþµÄ·½·¨

    jQuery+ajax¶ÁÈ¡²¢½âÎöXMLÎļþµÄ·½·¨

    2018-01-25 08:20

ÍøÓѵãÆÀ
Ï