locked
How to Connect to a Specific Windows app Client using Websockets from ashx Websocket Sever in vb.net

    Question

  • I'm developing a Windows Store app Card Game using websockets to connect each of two players.

    I currently have created an ashx page that works as an EchoWebSocket Server. My problem is that I want the server to push a simple text to player one when Player two takes there turn. I can't figure out how to keep track of the websocket connections so I can send the message from the server to a specific Players websocket connection

    Since it is an ashx there is no front end (ex Listview control) to keep the websockets in. also I can't seem to find where the socket addresses can be accessed through. there is no 'WebSocket.LocalEndPoint' or anything like that. so where do I find the addresses to store.

    Does anyone know of any good vb.net or c# Samples or tutorials of this. or can anyone explain.

    Below I included a copy of the ashx echo server I created so far. It works well for just checking the database for a record and pushing the text back down to the client when the data is changes but it uses a loop and I want to write a section into the server that determines the players in the game then only checks the database when the other player takes there turn. eliminating the loop. and saving bandwidth.

    <%@ WebHandler Language="VB" Class="EchoWebSocket" %>
    
    
    
    Imports System.Web
    Imports System.Net.WebSockets
    Imports System.Web.WebSockets
    Imports System.Runtime.InteropServices
    Imports System.Text
    Imports System.Threading
    Imports System.Threading.Tasks
    Imports MySql.Data.MySqlClient
    
    Public Class EchoWebSocket
    	Implements IHttpHandler
    	Private Const MaxBufferSize As Integer = 64 * 1024
    
        Public Sub ProcessRequest(context As HttpContext) Implements IHttpHandler.ProcessRequest
           
            Try
                context.AcceptWebSocketRequest(Async Function(wsContext)
    
                                                   Try
                                                       Dim receiveBuffer As Byte() = New Byte(MaxBufferSize - 1) {}
                                                       Dim buffer As New ArraySegment(Of Byte)(receiveBuffer)
                                                       Dim socket As WebSocket = wsContext.WebSocket
                                                       Dim userString As String
    
                                                       'socket.State.GetType.RemoteAddress or local endpoint something like that
                                                       
                                                       
                                                       If socket.State = WebSocketState.Open Then
                                                           ' Announcement when connected
                                                           Dim announceString = "EchoWebSocket Connected at: " & DateTime.Now.ToString() & " Your Socket ID = " '& context.ApplicationInstance.Session.SessionID
                                                           Dim outputBuffer2 As New ArraySegment(Of Byte)(Encoding.UTF8.GetBytes(announceString))
                                                           Await socket.SendAsync(outputBuffer2, WebSocketMessageType.Text, True, CancellationToken.None)
                                                       
                                                       End If
                                   
                                                       ' Stay in loop while websocket is open
                                                       While socket.State = WebSocketState.Open
                                                           Dim receiveResult As WebSocketReceiveResult = Await socket.ReceiveAsync(buffer, CancellationToken.None)
    
                                                           If receiveResult.MessageType = WebSocketMessageType.Close Then
                                                               
                                                               ' Echo back code and reason strings 
                                                               Await socket.CloseAsync(receiveResult.CloseStatus.GetValueOrDefault(), receiveResult.CloseStatusDescription, CancellationToken.None)
                                                               Return
                                                           End If
    
                                                           Dim offset As Integer = receiveResult.Count
    
                                                           While receiveResult.EndOfMessage = False
                                                               receiveResult = Await socket.ReceiveAsync(New ArraySegment(Of Byte)(receiveBuffer, offset, MaxBufferSize - offset), CancellationToken.None)
                                                               offset += receiveResult.Count
                                                           End While
    
                                                           If receiveResult.MessageType = WebSocketMessageType.Text Then
                                                               Dim cmdString As String = Encoding.UTF8.GetString(receiveBuffer, 0, offset)
                                                               userString = cmdString
                                                               userString = "You said: """ & userString & """"
    
                                                               Dim outputBuffer As New ArraySegment(Of Byte)(Encoding.UTF8.GetBytes(userString))
    
                                                               Await socket.SendAsync(outputBuffer, WebSocketMessageType.Text, True, CancellationToken.None)
                                                           ElseIf receiveResult.MessageType = WebSocketMessageType.Binary Then
                                                               userString = [String].Format("binary message received, size={0} bytes", receiveResult.Count)
    
                                                               Dim outputBuffer As New ArraySegment(Of Byte)(Encoding.UTF8.GetBytes(userString))
    
                                                               Await socket.SendAsync(outputBuffer, WebSocketMessageType.Text, True, CancellationToken.None)
                                                           End If
                                                           Dim cmdString1 As String = Encoding.UTF8.GetString(receiveBuffer, 0, offset)
                                                           Dim anstring As String = newmessagetosend(cmdString1).ToString
                                                           Do
                                                               anstring = newmessagetosend(cmdString1).ToString
                                                           Loop Until anstring.Contains(cmdString1.ToString)
    
                                                           Dim outbuff2 As New ArraySegment(Of Byte)(Encoding.UTF8.GetBytes(newmessagetosend(cmdString1)))
                                                          
                                                           Await socket.SendAsync(outbuff2, WebSocketMessageType.Text, True, CancellationToken.None)
                                                           
                                                       End While
                                                   Catch ex As Exception
                                                       System.Diagnostics.Trace.WriteLine(ex)
                                                   End Try
                                                   
                                               End Function)
            Catch ex As Exception
           
                System.Diagnostics.Trace.WriteLine(ex)
                context.Response.StatusCode = 500
                context.Response.StatusDescription = ex.Message
                context.Response.[End]()
            End Try 'try
    
        End Sub
    
    	Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
    		Get
    			Return False
    		End Get
        End Property
        
        Private Function newmessagetosend(FndGMID As String) As String
            
            Dim SQLConnect As String = "Server=myserver; User Id=MyUser; Password=MyPassword;Database=MyDB"
            
            Dim Enemy = New String(10) {}
            Dim FriendGameID As String = StringToArray(FndGMID)(0).ToString
            Dim FirendPlayerID As String = StringToArray(FndGMID)(1).ToString
            Try
    
    
                Using con As New MySqlConnection(SQLConnect)
                    con.Open()
                    Using cmd As MySqlCommand = con.CreateCommand()
    
    
    
                        cmd.CommandText = "select `GameID`,`PlayerID`,`TurnID`,`CharacterID`,`CP`,`Stamina`,`Recovery`,`AttackType`,`AttackValue`,`AName`,`NewGameFlag` FROM `Toons` Where `GameID` = " & "'" & FriendGameID & "' And `PlayerID` = " & "'" & FirendPlayerID & "'"
    
                        Using reader As MySqlDataReader = cmd.ExecuteReader()
                            If Not reader.Read() Then
                                Enemy(0) = "NoData"
                                Enemy(1) = "0" '"PlayerID"
                                Enemy(2) = "0" '"TurnID"
                                Enemy(3) = "0" '"CharacterID"
                                Enemy(4) = "0" '"CP"
                                Enemy(5) = "0" '"STamina"
                                Enemy(6) = "0" '"Recovery"
                                Enemy(7) = "0" '"AttackType"
                                Enemy(8) = "0" '"AttackValue"
                                Enemy(9) = "0" '"AName"
    
    
                                Return Enemy(0).ToString
    
                            Else
                                Enemy(0) = reader.GetString(0) 'GameID
                                Enemy(1) = reader.GetString(1) 'PlayerID
                                If Enemy(1) = "" Then
                                    Enemy(1) = "0"
                                End If
                                Enemy(2) = reader.GetString(2) 'TurnID
                                Enemy(3) = reader.GetString(3) 'CharacterID
                                Enemy(4) = reader.GetString(4) 'CP
                                Enemy(5) = reader.GetString(5) 'Stamina
                                Enemy(6) = reader.GetString(6) 'Recovery
                                Enemy(7) = reader.GetString(7) 'AttackType
                                Enemy(8) = reader.GetString(8) 'AttackValue
                                Enemy(9) = reader.GetString(9) 'AName
                                Enemy(10) = reader.GetString(10) 'NewGameFlag
    
                            End If
    
                            Dim stval As String = String.Join(",", Enemy)
                            Return stval
    
                            '  Return Enemy(0).ToString & " , " & Enemy(1).ToString & " , " & Enemy(2).ToString & " , " & Enemy(3).ToString & " , " & Enemy(4).ToString & " , " & Enemy(5).ToString & " , " & Enemy(6).ToString & " , " & Enemy(7).ToString & " , " & Enemy(8).ToString & " , " & Enemy(9).ToString & " , " & Enemy(10).ToString
                                
    
                            reader.Close()
    
                        End Using
                    End Using
                End Using
            Catch ex As Exception
    
            End Try
            
            Return Enemy(0)
    
            
            
            'Return "Player 2 Attacked you!!"
        End Function
        
        Private Function StringToArray(IncommingString As String) As String()
            
            Dim NewArray As String() = IncommingString.Split(",")
            Return NewArray
            
        End Function
        
        
        Private Sub sendMessageToAll(message As String)
            
           
            
            
        End Sub
        
    End Class

    Friday, May 30, 2014 3:44 PM

All replies

  • ok 'I THINK' I found the websocket id under 'context.Request.LogonUserIdentity.Token' but now how do I tell 'WebSocket.SendAsync' to send the message to a specific Token?
    Friday, May 30, 2014 7:32 PM