Hi,
I developed webSocket communication: client developed in HTML5 and server in xojo.
All works fine, but I can’t send messages from server to client.
I read on the internet that: data frames are delimited by the bytes 0x00 e 0xFF and have UTF-8 text between them
I don’t know how convert 0x00 and 0xFF to bytes and pass them to Write() method of TCPSocket.
I should write something like this:
I found a c# code on the internet, but I don’t know how to convert it in xojo language.
private void SendDataToClient(byte[] toSendBack)
{
//data frames are delimeted by the bytes 0x00 e 0xFF and
//have UTF-8 text between them
try
{
_socketClient.Send(BitConverter.GetBytes(0x00));
_socketClient.Send(toSendBack);
_socketClient.Send(BitConverter.GetBytes(0xFF));
}
catch
{
Shutdown();
}
}
I have been doing IPC and sockets for years and the convention of sending 0x00 and then the data followed by a 0xFF is just a convention that the developer of the C# code implemented. There are no actual conventions except the one(s) that you decide to implement.
I am just starting with Xojo so I cannot show you some XoJo code but I can show you the multi-platform C IPC code I have been using for years.
The IPC API’s work on any Windows version 95 >, any Linux, any Unix and any version of Mac OS X. The IPC API’s also do QNX message passing, you can ignore those parts of the code.
Also here is also a function I use to pass decimal and it returns the binary value.
Private Function mConvertDecToBin(InputString as String) as String
// Convert Input String from Decimal into ASCII Binary
Dim Process as String
Process = ChrB(Val(InputString))
Return Process
End Function
Cristian,
Here is a function I wrote to build a TELNET ECHO message response.
I call this function and return the “packed frame(s)” in preparation for the “Write” to the socket. In this example I am using a bunch of Constants to represent decimal values. For example “cIAC” is 255, “cDO” is 253, and “cECHO” is 01. After this function happens the frame for example has a binary value of FFFD01. HTH. In this case I know already that my frame size has to be and is 3 Bytes long.
Private Function mBuild_ECHO_ResponseFrames(OptionVerb as String) As string
Dim Response As String
If OptionVerb = Str(cWILL) Then
If StateBit_ECHO_InMode = False Then
If UserEcho = True Then
Response = ChrB(cIAC) + ChrB(cDO) + ChrB(cECHO)
StateBit_ECHO_InMode = True
Elseif UserEcho = False Then
Response = ChrB(cIAC) + ChrB(cDONT) + ChrB(cECHO)
StateBit_ECHO_InMode = False
End If
Elseif StateBit_ECHO_InMode = True Then
// Do Nothing as we are in Mode already
End If
Elseif OptionVerb = Str(cDO) Then
If StateBit_ECHO_InMode = False Then
If UserEcho = True Then
Response = ChrB(cIAC) + ChrB(cWILL) + ChrB(cECHO)
StateBit_ECHO_InMode = True
Elseif UserEcho = False Then
Response = ChrB(cIAC) + ChrB(cWONT) + ChrB(cECHO)
End If
Elseif StateBit_ECHO_InMode = True Then
// Do Nothing as we are in Mode already
End If
Elseif OptionVerb = Str(cWONT) Then
// Do not Reply as server does not want this mode
Elseif OptionVerb = Str(cDONT) Then
// Do not Reply as server does not want this mode
End If
Return Response
Response = ""
End Function