Skip to content

Instantly share code, notes, and snippets.

@digitallysavvy
Last active December 3, 2019 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digitallysavvy/14b6cd3c1e961974f874f3ccb00f6ebe to your computer and use it in GitHub Desktop.
Save digitallysavvy/14b6cd3c1e961974f874f3ccb00f6ebe to your computer and use it in GitHub Desktop.
A snippet showing how to handle receiving a DataStream using Agora's Video SDK (ARSupportBroadcastViewController)
func rtcEngine(_ engine: AgoraRtcEngineKit, receiveStreamMessageFromUid uid: UInt, streamId: Int, data: Data) {
// successfully received message from user
guard let dataAsString = String(bytes: data, encoding: String.Encoding.ascii) else { return }
if debug {
print("STREAMID: \(streamId)\n - DATA: \(data)\n - STRING: \(dataAsString)\n")
}
// check data message
switch dataAsString {
case var dataString where dataString.contains("color:"):
if debug {
print("color msg recieved\n - \(dataString)")
}
// remove the [ ] characters from the string
if let closeBracketIndex = dataString.firstIndex(of: "]") {
dataString.remove(at: closeBracketIndex)
dataString = dataString.replacingOccurrences(of: "color: [", with: "")
}
// convert the string into an array -- using , as delimeter
let colorComponentsStringArray = dataString.components(separatedBy: ", ")
// safely convert the string values into numbers
guard let redColor = NumberFormatter().number(from: colorComponentsStringArray[0]) else { return }
guard let greenColor = NumberFormatter().number(from: colorComponentsStringArray[1]) else { return }
guard let blueColor = NumberFormatter().number(from: colorComponentsStringArray[2]) else { return }
guard let colorAlpha = NumberFormatter().number(from: colorComponentsStringArray[3]) else { return }
// set line color to UIColor from remote user
self.lineColor = UIColor.init(red: CGFloat(truncating: redColor), green: CGFloat(truncating: greenColor), blue: CGFloat(truncating:blueColor), alpha: CGFloat(truncating:colorAlpha))
case "undo":
// TODO: add undo
case "touch-start":
// touch-starts
print("touch-start msg recieved")
// TODO: handle event
case "touch-end":
if debug {
print("touch-end msg recieved")
}
default:
if debug {
print("touch points msg recieved")
}
// TODO: add points in ARSCN
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment