Answered by:
Can't Get MessageWebSocket.MessageReceived working from microsoft sample

Question
-
I am trying to follow Microsoft's Samples on MessageWebsockets . the samples are in c# and I'm trying to learn it in vb.net for a windows store app.
My problem is two lines of the code don't work
webSocket.MessageReceived += MessageReceived()
and
webSocket.Closed += Closed()
I get errors saying
Error 1 'Public Event MessageReceived(sender As Windows.Networking.Sockets.MessageWebSocket, args As Windows.Networking.Sockets.MessageWebSocketMessageReceivedEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.
and
Error 4 'Public Event Closed(sender As Windows.Networking.Sockets.IWebSocket, args As Windows.Networking.Sockets.WebSocketClosedEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.
How can I get this working (again: in vb.net building a Windows Store app)
you would think since this is a microsoft sample in Dev Center for windows Store apps, it would work as is. I have the following imports(maybe I'm Missing one as the aren't included in the sample)
Imports Windows.Networking.Sockets
Imports Windows.Storage.Streams
Imports Windows.Web
Imports System.ThreadingI'm using VS Express 2013 for Windows
Sunday, May 18, 2014 5:53 PM
Answers
-
For VB you'll need to wire up the event with AddHandler rather than by using the C# += syntax. I'm not on a dev machine to confirm, but it will be something like AddHandler webSocket.MessageReceived, AddressOf MessageReceived
- Marked as answer by Jamles HezModerator Tuesday, May 27, 2014 9:15 AM
Sunday, May 18, 2014 6:09 PMModerator
All replies
-
For VB you'll need to wire up the event with AddHandler rather than by using the C# += syntax. I'm not on a dev machine to confirm, but it will be something like AddHandler webSocket.MessageReceived, AddressOf MessageReceived
- Marked as answer by Jamles HezModerator Tuesday, May 27, 2014 9:15 AM
Sunday, May 18, 2014 6:09 PMModerator -
ok I've tried what you said. however I am not at a point where I can test it. (it give no ide errors) Does this look correct?
Private Async Function Start_Click(sender As Object, e As RoutedEventArgs) As Task Try ' Make a local copy to avoid races with Closed events. Dim webSocket As MessageWebSocket = messageWebSocket ' Have we connected yet? If webSocket Is Nothing Then Dim server As New Uri(ServerAddressField.Text.Trim()) webSocket = New MessageWebSocket() ' MessageWebSocket supports both utf8 and binary messages. ' When utf8 is specified as the messageType, then the developer ' promises to only send utf8-encoded data. webSocket.Control.MessageType = SocketMessageType.Utf8 ' Set up callbacks 'This is what I changed 'webSocket.MessageReceived += MessageReceived() 'webSocket.Closed += Closed() AddHandler webSocket.MessageReceived, MessageReceived(webSocket, webSocket.OutputStream) AddHandler webSocket.Closed, Closed(webSocket, webSocket.OutputStream) 'This ends what I changed Await webSocket.ConnectAsync(server) messageWebSocket = webSocket ' Only store it after successfully connecting. messageWriter = New DataWriter(webSocket.OutputStream) End If Dim message As String = InputField.Text ' Buffer any data we want to send. messageWriter.WriteString(message) ' Send the data as one complete message. Await messageWriter.StoreAsync() Catch ex As Exception ' For debugging ' Add your specific error-handling code here. Dim status As WebErrorStatus = WebSocketError.GetStatus(ex.GetBaseException().HResult) End Try End Function 'Receive Data Private Function MessageReceived(sender As MessageWebSocket, args As MessageWebSocketMessageReceivedEventArgs) As Object Try Using reader As DataReader = args.GetDataReader() reader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8 Dim read As String = reader.ReadString(reader.UnconsumedBufferLength) End Using Catch ex As Exception ' For debugging ' Add your specific error-handling code here. Dim status As WebErrorStatus = WebSocketError.GetStatus(ex.GetBaseException().HResult) End Try End Function 'close connection Private Function Closed(sender As IWebSocket, args As WebSocketClosedEventArgs) As Object ' You can add code to log or display the code and reason ' for the closure (stored in args.Code and args.Reason) ' This is invoked on another thread so use Interlocked ' to avoid races with the Start/Close/Reset methods. Dim webSocket As MessageWebSocket = Interlocked.Exchange(messageWebSocket, Nothing) If webSocket IsNot Nothing Then webSocket.Dispose() End If End Function 'Websocket End Class
Sunday, May 18, 2014 8:20 PM -
Without actually running that it looks reasonable.Tuesday, May 20, 2014 6:10 PMModerator