Search code examples
vb.netnsq

NSQ vb.net MessageHandler


I am trying to use this package in vb.net NsqSharp There is a good code for it in C# but i need it in vb.net.

I got it to send a message to my NSQ server, but the problem is to get it. But i get a error on consumer.AddHandler(New HandleMessage()) and i do not know if i declare the HandleMessage right.

Imports NsqSharp
Imports System.IO
Imports System.Text
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim producer = New Producer("127.0.0.1:4150")

        producer.Publish("test-topic-name", Me.txt_tx.Text)
        producer.Stop()
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim consumer = New Consumer("test-topic-name", "channel-name")
        consumer.AddHandler(New HandleMessage())

        consumer.ConnectToNsqLookupd("127.0.0.1:4161")
        consumer.Stop()
    End Sub
    Public Interface IHandler : End Interface
    Public Sub HandleMessage(message As Message)
        Dim msg As String = Encoding.UTF8.GetString(message.Body)
        MsgBox(msg)
    End Sub
    Public Sub LogFailedMessage(message As Message)
        Dim msg As String = Encoding.UTF8.GetString(message.Body)
        MsgBox(msg)
    End Sub
End Class

Solution

  • But i get a error on Implements IHandler

    Lovely description of the problem, you can't get useful answers when you don't describe the exact error message you see. You did write the code wrong, VB.NET requires the Implements keyword on interface method implementations. You'd normally fall in the pit of success by letting the IDE generate these methods for you. As soon as you type "Implements IHandler" and press the Enter key, the IDE automagically adds the methods.

    So there's probably something wrong with the library reference as well. Steps one-by-one:

    1. Tools > Nuget Package Manager > Package Manager Console.
    2. Type "Install-Package NsqSharp". Watch it trundle while it downloads and installs the package.
    3. Put Imports NsqSharp at the top of the source file.

    You should now end up with:

    Public Class MessageHandler
        Implements IHandler
    
        Private Sub IHandler_HandleMessage(message As Message) Implements IHandler.HandleMessage
            Dim msg As String = Encoding.UTF8.GetString(message.Body)
            MessageBox.Show(msg)
        End Sub
    
        Private Sub IHandler_LogFailedMessage(message As Message) Implements IHandler.LogFailedMessage
            Dim msg As String = Encoding.UTF8.GetString(message.Body)
            MessageBox.Show(msg)
        End Sub
    End Class