public void Apply(BinaryInfo binaryInfo) { var messageJson = binaryInfo.jsonFormat; long id = (long)messageJson["id"]; LogUtilty.log("received id: " + id); FileStream fileStream; if (fileStreams.ContainsKey(id)) { fileStream = fileStreams[id]; } else { fileStream = new FileStream(@"D:\tmp\okay test\" + messageJson["fileName"], FileMode.Create, FileAccess.Write); fileStreams.Add(id, fileStream); LogUtilty.log("Created new file stream for location:" + @"D:\tmp\" + messageJson["fileName"]); } long frame = (long)messageJson["frame"]; long latestFrame = (long)messageJson["latestFrame"]; byte[] binaryData = binaryInfo.binaryData; fileStream.Write(binaryData, binaryInfo.lengthToBody, binaryData.Length - binaryInfo.lengthToBody); if (frame == latestFrame) { fileStream.Close(); fileStreams.Remove(id); LogUtilty.log("Closed stream and finished file writing, and removed from map"); } }
protected override void OnMessage(MessageEventArgs messageEventArgs) { try { if (messageEventArgs.IsText) { string messageString = messageEventArgs.Data; var messageJson = JObject.Parse(messageString); CommandFactory.get(messageJson["type"].ToString()).Apply(Context.WebSocket, messageJson); } else if (messageEventArgs.IsBinary) { byte[] messageBinary = messageEventArgs.RawData; var binaryInformation = getBinaryInformation(messageBinary); BinaryCommandFactory.apply(binaryInformation); } } catch (Exception e) { LogUtilty.log("Something went wrong: " + e); } }