2012-03-20 17 views
5

सिग्नलर क्लाइंट डिस्कनेक्शन को कैसे संभालता है? क्या मैं सही हूं यदि मैं निम्नलिखित बताता हूं?सिग्नलआर: क्लाइंट डिस्कनेक्शन

  • सिग्नलआर जावास्क्रिप्ट ईवेंट हैंडलिंग के माध्यम से ब्राउजर पेज को बंद/रीफ्रेश करेगा और सर्वर (उचित कनेक्शन के माध्यम से) को उचित पैकेट भेज देगा;
  • सिग्नलआर ब्राउज़र बंद/नेटवर्क विफलता का पता नहीं लगाएगा (शायद केवल टाइमआउट द्वारा)।

मेरा लक्ष्य लंबे मतदान वाले परिवहन का लक्ष्य है।

मुझे this question से अवगत है, लेकिन यह मेरे लिए थोड़ा स्पष्ट करना चाहता है।

उत्तर

9

यदि कोई उपयोगकर्ता पृष्ठ को रीफ्रेश करता है, तो उसे एक नया कनेक्शन माना जाता है। आप सही हैं कि डिस्कनेक्ट टाइमआउट पर आधारित है।

आप SignalR.Hubs.IConnected और SignalR.Hubs.IDisconnect लागू करके एक हब में कनेक्ट/रिकनेक्ट और डिस्कनेक्ट ईवेंट को संभाल सकते हैं।

ऊपर उल्लिखित सिग्नल 0.5.x.

public class ContosoChatHub : Hub 
{ 
    public override Task OnConnected() 
    { 
     // Add your own code here. 
     // For example: in a chat application, record the association between 
     // the current connection ID and user name, and mark the user as online. 
     // After the code in this method completes, the client is informed that 
     // the connection is established; for example, in a JavaScript client, 
     // the start().done callback is executed. 
     return base.OnConnected(); 
    } 

    public override Task OnDisconnected() 
    { 
     // Add your own code here. 
     // For example: in a chat application, mark the user as offline, 
     // delete the association between the current connection id and user name. 
     return base.OnDisconnected(); 
    } 

    public override Task OnReconnected() 
    { 
     // Add your own code here. 
     // For example: in a chat application, you might have marked the 
     // user as offline after a period of inactivity; in that case 
     // mark the user as online again. 
     return base.OnReconnected(); 
    } 
} 
6

SignalR 1.0 में, SignalR.Hubs.IConnected और SignalR.Hubs.IDisconnect नहीं रह गया है लागू किया जाता है, और अब यह सिर्फ पर एक ओवरराइड है:

the official documentation (v1.1.3 के लिए वर्तमान में) से

हब खुद:

public class Chat : Hub 
{ 
    public override Task OnConnected() 
    { 
     return base.OnConnected(); 
    } 

    public override Task OnDisconnected() 
    { 
     return base.OnDisconnected(); 
    } 

    public override Task OnReconnected() 
    { 
     return base.OnReconnected(); 
    } 
}