Imports System
Imports System.Text
Imports Amqp
Imports Amqp.Sasl
Imports Amqp.Framing
Imports System.Threading
Imports System.Security.Cryptography.X509Certificates
Imports System.Net.Security
Imports System.Security.Cryptography
Namespace amqp
Class MainClass
Shared Host As String = "${YourHost}"
Shared Port As Integer = 5671
Shared AccessKey As String = "${YourAccessKey}"
Shared AccessSecret As String = "${YourAccessSecret}"
Shared consumerGroupId As String = "${YourConsumerGroupId}"
Shared clientId As String = "${YourClientId}"
Shared iotInstanceId As String = "${YourIotInstanceId}"
Shared Count As Integer = 0
Shared IntervalTime As Integer = 10000
Shared address As Address
Public Shared Sub Main(ByVal args As String())
Dim timestamp As Long = GetCurrentMilliseconds()
Dim param As String = "authId=" & AccessKey & "×tamp=" & timestamp
Dim userName As String = clientId & "|authMode=aksign,signMethod=hmacmd5,c,iotInstanceId=" & iotInstanceId & ",authId=" & AccessKey & ",timestamp=" & timestamp & "|"
Dim password As String = doSign(param, AccessSecret, "HmacMD5")
DoConnectAmqp(userName, password)
Dim resetEvent As ManualResetEvent = New ManualResetEvent(False)
resetEvent.WaitOne()
End Sub
Private Shared Sub DoConnectAmqp(ByVal userName As String, ByVal password As String)
address = New Address(Host, Port, userName, password)
Dim cf As ConnectionFactory = New ConnectionFactory()
cf.SASL.Profile = SaslProfile.External
cf.AMQP.IdleTimeout = 120000
cf.AMQP.C
cf.AMQP.HostName = "contoso.com"
cf.AMQP.MaxFrameSize = 8 * 1024
Dim connection = cf.CreateAsync(address).Result
connection.AddClosedCallback(AddressOf ConnClosed)
DoReceive(connection)
End Sub
Private Shared Sub DoReceive(ByVal connection As Connection)
Dim session = New Session(connection)
Dim receiver = New ReceiverLink(session, "queueName", Nothing)
receiver.Start(20, Function(link, message)
Dim messageId As Object = message.ApplicationProperties("messageId")
Dim topic As Object = message.ApplicationProperties("topic")
Dim body As String = Encoding.UTF8.GetString(CType(message.Body, Byte()))
Console.WriteLine("receive message, topic=" & topic & ", messageId=" & messageId & ", body=" & body)
link.Accept(message)
End Function)
End Sub
Private Shared Sub ConnClosed(ByVal __ As IAmqpObject, ByVal e As [Error])
Console.WriteLine("ocurr error: " & e)
If Count < 3 Then
Count += 1
Thread.Sleep(IntervalTime * Count)
Else
Thread.Sleep(120000)
End If
DoConnectAmqp(address.User, address.Password)
End Sub
Private Shared Function GetCert() As X509Certificate
Dim certPath As String = Environment.CurrentDirectory & "/root.crt"
Dim crt As X509Certificate = New X509Certificate(certPath)
Return crt
End Function
Private Shared Function ValidateServerCertificate(ByVal sender As Object, ByVal certificate As X509Certificate, ByVal chain As X509Chain, ByVal sslPolicyErrors As SslPolicyErrors) As Boolean
Return True
End Function
Private Shared Function GetCurrentMilliseconds() As Long
Dim dt1970 As DateTime = New DateTime(1970, 1, 1)
Dim current As DateTime = DateTime.Now
Return CLng((current - dt1970).TotalMilliseconds)
End Function
Private Shared Function doSign(ByVal param As String, ByVal accessSecret As String, ByVal signMethod As String) As String
Dim key As Byte() = Encoding.UTF8.GetBytes(accessSecret)
Dim signContent As Byte() = Encoding.UTF8.GetBytes(param)
Dim hmac = New HMACMD5(key)
Dim hashBytes As Byte() = hmac.ComputeHash(signContent)
Return Convert.ToBase64String(hashBytes)
End Function
End Class
End Namespace