Created
January 19, 2012 08:48
-
-
Save codemasta92/1638836 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void ProcessFrame(FrameType frameType, byte[] data, Action<string> onMessage, Action onClose, | |
Action<byte[]> onBinary) | |
{ | |
switch (frameType) | |
{ | |
case FrameType.Close: | |
if (data.Length == 2) | |
{ | |
var closeCode = (uint)data.Take(2).ToArray().ToLittleEndianInt(); | |
switch (closeCode) | |
{ | |
case 1000: | |
// Do something | |
break; | |
case 1001: | |
// Do something | |
break; | |
case 1002: | |
// Do something | |
break; | |
case 1003: | |
// Do something | |
break; | |
default: | |
throw new WebSocketException(WebSocketStatusCodes.ProtocolError); | |
} | |
} | |
onClose(); | |
break; | |
case FrameType.Binary: | |
onBinary(data); | |
break; | |
case FrameType.Text: | |
var encoding = new UTF8Encoding(false, true); | |
try | |
{ | |
var message = encoding.GetString(data); | |
onMessage(message); | |
} | |
catch (ArgumentException) | |
{ | |
throw new WebSocketException(WebSocketStatusCodes.InvalidFramePayloadData); | |
} | |
break; | |
default: | |
FleckLog.Debug("Received unhandled " + frameType); | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment