服务器关闭时启动期间的奇怪连接行为 [英] Strange connections behaviour during its initiation when server is down
问题描述
Damian Edwards(signalR专家),并且我成功地实现了它. while(true)内只有一个问题 我的Connect()方法的这一行:
I've followed Damian Edwards (signalR expert) on channel 9 about signalR and i succesfully implemented it on my side. There is just one concern within while(true) of my Connect() method, this line:
Await Task.Delay(time)
他把它放在那里等待下一次调用,以免向服务器发送这么多请求.假设服务器已关闭,而我的客户端应用程序正在尝试连接该服务器.如果我不在while循环中包含上述说明,那么一切似乎都很好,但是 不知何故,当我将其放置在此处时,我收到的消息比我想象的要多(请看底部提供的屏幕截图.
He put it there to wait before next call to not send so many requests towards server. Let's assume the server is down and my client application is trying to conenct to it. If i not include above instruction within while loop everything seems to be fine, however somehow when i place it there i get more messages than i think it should be (look at the screenshoots i included at the bottom.
谁能向我解释这种行为?这是我的全部代码:
Can anyone explain me this behaviour? This is my entire code:
Public Class Form1
Private Const _hubUrl As String = "http://localhost:4848"
Private _hubConnection As HubConnection
Private _hubProxy As IHubProxy
Private _connectionStateLocker As New Object()
Private _msgLocker As New Object()
Private _canSend As Boolean
Private ReadOnly _reconnectDelay As TimeSpan = TimeSpan.FromSeconds(3)
Private Async Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
WriteToFile("Waiting to connect...")
Await Connect()
Catch ex As Exception
WriteToFile("Form1_Load: " + ex.ToString())
End Try
End Sub
Private Sub WriteToFile(text As String)
SyncLock _msgLocker
Dim strFile As String = String.Format("C:\Users\JAR\Desktop\Log.txt", DateTime.Today.ToString("dd-MMM-yyyy"))
File.AppendAllText(strFile, String.Format("{0}>>{1}{2}", DateTime.Now, text, Environment.NewLine))
End SyncLock
End Sub
Async Function Connect() As Task
SyncLock _connectionStateLocker
'this is protect against connecting for only once..
If _hubConnection IsNot Nothing AndAlso _hubConnection.State <> ConnectionState.Disconnected Then
Return
End If
_hubConnection = New HubConnection(_hubUrl)
AddHandler _hubConnection.Reconnecting, Sub()
If _hubConnection.State = ConnectionState.Connected Then
_canSend = False
WriteToFile("Connection reconnecting")
End If
End Sub
AddHandler _hubConnection.StateChanged, Sub(e)
If e.OldState = ConnectionState.Reconnecting AndAlso e.NewState = ConnectionState.Connected Then
WriteToFile(String.Format("Connected to {0} via {1}", _hubUrl, _hubConnection.Transport.Name))
_canSend = True
End If
End Sub
AddHandler _hubConnection.Closed, Async Sub()
_canSend = False
WriteToFile("Connection lost, reconnecting in a bit...")
_canSend = True
Await Task.Delay(_reconnectDelay)
Await Connect() 'looks we re-entred recursive this method but actually we isn't because we inside delegate here (Async Sub()...) (annonymous delegate) so if it's called here we are not in THIS METHOD Connect but new one
End Sub
_hubProxy = _hubConnection.CreateHubProxy("Main")
_hubProxy.[On](Of HelloModel)("sendHelloObject", Sub(hello)
WriteToFile("Recieved sendHelloObject: Molly: " + hello.Molly + " Age: " + hello.Age.ToString())
End Sub)
WriteToFile("Connecting...")
End SyncLock
'Keep trying to connect until it works (dont just call Start expect to work as one time server could not be available)
'so what we gonna to do is retry the loop over and over again until such time it works.
While True
Try
Await _hubConnection.Start()
If _hubConnection.State = ConnectionState.Connected Then
WriteToFile(String.Format("Connected to {0} via {1}", _hubUrl, _hubConnection.Transport.Name))
_canSend = True
Exit While
End If
Catch ex As Exception
WriteToFile(String.Format("Connecting to {0} failed, trying again in a bit", _hubUrl)) '+ ex.Message)
End Try
' Await Task.Delay(_reconnectDelay) <--------
End While
End Function
从控制台转储以查看其外观:
Dump from console to see how it looks:
没有等待Task.Delay(3000):
Without await Task.Delay(3000):
等待Task.Delay(3000):
With await Task.Delay(3000):
推荐答案
Maybe Damian Edwards can explain it.
另一方面,如果服务器宕机并且不是由于手动原因引起的,则有人需要修复服务器和/或网络,并且/或者任何可能导致该问题发生的问题,因为它应该不多除非整个IT部门都在任何地方解决问题 出去吃午餐.
On the other hand if a server is down and it is not due to manual cause somebody needs to fix the server and/or the network and/or whatever problem(s) would cause that to occur as it should not be much of an issue anywhere unless the entire IT department is out to lunch.
这篇关于服务器关闭时启动期间的奇怪连接行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!