chore: stitching everything together
parent
be0f9e2051
commit
ad22b0b718
|
@ -15,6 +15,11 @@ import Flutter
|
|||
(call: FlutterMethodCall, result: FlutterResult) -> Void in
|
||||
|
||||
switch call.method {
|
||||
case "init":
|
||||
if let authToken: String = call.arguments as? String {
|
||||
peerManager.initializeToken(authToken: authToken)
|
||||
print("Initialized")
|
||||
}
|
||||
case "join":
|
||||
if let conversationId: String = call.arguments as? String {
|
||||
peerManager.join(conversationId: conversationId)
|
||||
|
|
|
@ -99,6 +99,7 @@ class PeerConnectionWrapper: NSObject{
|
|||
print(error)
|
||||
} else {
|
||||
// handle the remote sdp
|
||||
this.handleRemoteDescriptionSet()
|
||||
this.state = .ready
|
||||
}
|
||||
})
|
||||
|
@ -117,7 +118,7 @@ class PeerConnectionWrapper: NSObject{
|
|||
print(error)
|
||||
} else {
|
||||
// handle the remote sdp
|
||||
|
||||
this.handleRemoteDescriptionSet()
|
||||
// create answer
|
||||
peerConnection.answer(for: this.channelConstraint, completionHandler:
|
||||
{ (sdp, error) in
|
||||
|
@ -158,6 +159,13 @@ private extension PeerConnectionWrapper {
|
|||
self.peerConnection = self.connectionFactory?.peerConnection(with: configuration, constraints: self.connectionConstraint, delegate: self)
|
||||
}
|
||||
|
||||
func handleRemoteDescriptionSet() {
|
||||
for iceCandidate in self.remoteIceCandidates {
|
||||
self.peerConnection?.add(iceCandidate)
|
||||
}
|
||||
self.remoteIceCandidates = []
|
||||
}
|
||||
|
||||
func localStream() -> RTCMediaStream {
|
||||
let factory = self.connectionFactory!
|
||||
let localStream = factory.mediaStream(withStreamId: "RTCmS")
|
||||
|
@ -171,27 +179,27 @@ private extension PeerConnectionWrapper {
|
|||
|
||||
extension PeerConnectionWrapper: RTCPeerConnectionDelegate {
|
||||
func peerConnectionShouldNegotiate(_ peerConnection: RTCPeerConnection) {
|
||||
<#code#>
|
||||
|
||||
}
|
||||
|
||||
func peerConnection(_ peerConnection: RTCPeerConnection, didChange stateChanged: RTCSignalingState) {
|
||||
<#code#>
|
||||
|
||||
}
|
||||
|
||||
func peerConnection(_ peerConnection: RTCPeerConnection, didAdd stream: RTCMediaStream) {
|
||||
<#code#>
|
||||
print("adding new stream from remote")
|
||||
}
|
||||
|
||||
func peerConnection(_ peerConnection: RTCPeerConnection, didRemove stream: RTCMediaStream) {
|
||||
<#code#>
|
||||
|
||||
}
|
||||
|
||||
func peerConnection(_ peerConnection: RTCPeerConnection, didChange newState: RTCIceConnectionState) {
|
||||
<#code#>
|
||||
|
||||
}
|
||||
|
||||
func peerConnection(_ peerConnection: RTCPeerConnection, didChange newState: RTCIceGatheringState) {
|
||||
<#code#>
|
||||
|
||||
}
|
||||
|
||||
func peerConnection(_ peerConnection: RTCPeerConnection, didGenerate candidate: RTCIceCandidate) {
|
||||
|
@ -200,10 +208,10 @@ extension PeerConnectionWrapper: RTCPeerConnectionDelegate {
|
|||
}
|
||||
|
||||
func peerConnection(_ peerConnection: RTCPeerConnection, didRemove candidates: [RTCIceCandidate]) {
|
||||
<#code#>
|
||||
|
||||
}
|
||||
|
||||
func peerConnection(_ peerConnection: RTCPeerConnection, didOpen dataChannel: RTCDataChannel) {
|
||||
<#code#>
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ class PeerManager: NSObject {
|
|||
// WebRTC initialization
|
||||
var connectionFactory: RTCPeerConnectionFactory?
|
||||
var signalingApiProvider: SignalingApiProvider?
|
||||
var eventSource: EventSource = EventSource(url: "https://staging.beepvoice.app/signal")
|
||||
var eventSource: EventSource = EventSource(url: "http://localhost/signal/subscribe")
|
||||
|
||||
// List of users
|
||||
var peerList: [String: PeerConnectionWrapper] = [:]
|
||||
|
@ -22,7 +22,13 @@ class PeerManager: NSObject {
|
|||
|
||||
public override init() {
|
||||
super.init()
|
||||
initialisePeerConnectionFactory()
|
||||
RTCPeerConnectionFactory.initialize()
|
||||
self.connectionFactory = RTCPeerConnectionFactory()
|
||||
}
|
||||
|
||||
// MUST CALL THIS BEFORE SIGNSLLING WORKS
|
||||
public func initializeToken(authToken: String) {
|
||||
self.signalingApiProvider = SignalingApiProvider(authToken: authToken)
|
||||
}
|
||||
|
||||
public func join(conversationId: String) {
|
||||
|
@ -68,16 +74,12 @@ class PeerManager: NSObject {
|
|||
}
|
||||
|
||||
private extension PeerManager {
|
||||
func initialisePeerConnectionFactory() {
|
||||
RTCPeerConnectionFactory.initialize()
|
||||
self.connectionFactory = RTCPeerConnectionFactory()
|
||||
}
|
||||
|
||||
func initialiseEventSource() {
|
||||
eventSource.addEventListener("offer") { (id, event, data) in
|
||||
|
||||
guard let id = id, let data = data else {
|
||||
// Incorrect packet type error
|
||||
return
|
||||
}
|
||||
|
||||
// Handling offers, if in list accept
|
||||
|
@ -104,6 +106,7 @@ private extension PeerManager {
|
|||
|
||||
guard let id = id, let data = data else {
|
||||
// Incorrect packet type error
|
||||
return
|
||||
}
|
||||
|
||||
// Handling answers, if in list accept
|
||||
|
|
|
@ -10,7 +10,7 @@ import Foundation
|
|||
import WebRTC
|
||||
|
||||
class SignalingApiProvider: NSObject {
|
||||
var authToken: String
|
||||
var authToken: String?
|
||||
|
||||
public override init() {
|
||||
super.init()
|
||||
|
@ -22,18 +22,16 @@ class SignalingApiProvider: NSObject {
|
|||
}
|
||||
|
||||
public func getUserDevices(userId: String) -> [String]? {
|
||||
let url: URL = URL(string: "http://staging.beepvoice.app/user/\(userId)/devices")!
|
||||
let url: URL = URL(string: "http://localhost/signal/user/\(userId)/devices")!
|
||||
var deviceList: [String] = []
|
||||
var request = URLRequest(url: url)
|
||||
request.addValue("Bearer \(authToken)", forHTTPHeaderField: "Authorization")
|
||||
request.addValue("Bearer \(authToken ?? "0")", forHTTPHeaderField: "Authorization")
|
||||
request.httpMethod = "GET"
|
||||
|
||||
let response: AutoreleasingUnsafeMutablePointer<URLResponse?>
|
||||
var response: URLResponse?
|
||||
|
||||
do {
|
||||
let dataVal = try NSURLConnection.sendSynchronousRequest(request, returning: response)
|
||||
|
||||
print(response)
|
||||
let dataVal = try NSURLConnection.sendSynchronousRequest(request, returning: &response)
|
||||
do {
|
||||
if let jsonResult = try JSONSerialization.jsonObject(with: dataVal, options: []) as? [String] {
|
||||
print("Synchronous\(jsonResult)")
|
||||
|
@ -58,18 +56,16 @@ class SignalingApiProvider: NSObject {
|
|||
}
|
||||
|
||||
public func getConversationUsers(conversationId: String) -> [String]? {
|
||||
let url: URL = URL(string: "http://staging.beepvoice.app/user/conversation/\(conversationId)/member")!
|
||||
let url: URL = URL(string: "http://localhost/core/user/conversation/\(conversationId)/member")!
|
||||
var userList: [String] = []
|
||||
var request = URLRequest(url: url)
|
||||
request.addValue("Bearer \(authToken)", forHTTPHeaderField: "Authorization")
|
||||
request.addValue("Bearer \(authToken ?? "0")", forHTTPHeaderField: "Authorization")
|
||||
request.httpMethod = "GET"
|
||||
|
||||
let response: AutoreleasingUnsafeMutablePointer<URLResponse?>
|
||||
var response: URLResponse?
|
||||
|
||||
do {
|
||||
let dataVal = try NSURLConnection.sendSynchronousRequest(request, returning: response)
|
||||
|
||||
print(response)
|
||||
let dataVal = try NSURLConnection.sendSynchronousRequest(request, returning: &response)
|
||||
do {
|
||||
if let jsonResult = try JSONSerialization.jsonObject(with: dataVal, options: []) as? [Any] {
|
||||
print("Synchronous\(jsonResult)")
|
||||
|
@ -99,7 +95,7 @@ class SignalingApiProvider: NSObject {
|
|||
|
||||
// CHECK FOR WHEN DEVICE IS UNAVAILABLE
|
||||
public func postDataToUser(userId: String, deviceId: String, data: String, event: String) {
|
||||
let url: URL = URL(string: "http://staging.beepvoice.app/user/\(userId)/device/\(deviceId)")!
|
||||
let url: URL = URL(string: "http://localhost/signal/user/\(userId)/device/\(deviceId)")!
|
||||
|
||||
// prepare json data
|
||||
let json: [String: Any] = ["event": event, "data": data]
|
||||
|
@ -107,15 +103,14 @@ class SignalingApiProvider: NSObject {
|
|||
let jsonData = try? JSONSerialization.data(withJSONObject: json)
|
||||
|
||||
var request = URLRequest(url: url)
|
||||
request.addValue("Bearer \(authToken)", forHTTPHeaderField: "Authorization")
|
||||
request.addValue("Bearer \(authToken ?? "0")", forHTTPHeaderField: "Authorization")
|
||||
request.httpMethod = "POST"
|
||||
request.httpBody = jsonData
|
||||
|
||||
let response: AutoreleasingUnsafeMutablePointer<URLResponse?>
|
||||
var response: URLResponse?
|
||||
|
||||
do {
|
||||
let _ = try NSURLConnection.sendSynchronousRequest(request, returning: response)
|
||||
print(response)
|
||||
let _ = try NSURLConnection.sendSynchronousRequest(request, returning: &response)
|
||||
} catch let error as NSError {
|
||||
print(error.localizedDescription)
|
||||
}
|
||||
|
|
|
@ -3,6 +3,19 @@ import "package:flutter/services.dart";
|
|||
|
||||
class ConversationManager {
|
||||
static const channel = const MethodChannel('beepvoice.app/conversation');
|
||||
static bool isInit = false;
|
||||
|
||||
static init(String authToken) async {
|
||||
if (isInit == false) {
|
||||
try {
|
||||
await channel.invokeMethod('init', authToken);
|
||||
isInit = true;
|
||||
} on PlatformException catch (e) {
|
||||
print(e);
|
||||
}
|
||||
}
|
||||
print("init");
|
||||
}
|
||||
|
||||
Future<int> join(String conversationId) async {
|
||||
try {
|
||||
|
|
|
@ -3,6 +3,7 @@ import "package:flutter_svg/flutter_svg.dart";
|
|||
|
||||
import "../../widgets/text_button.dart";
|
||||
import "../../../services/login_manager.dart";
|
||||
import "../../../services/conversation_manager.dart";
|
||||
import "phone_input.dart";
|
||||
|
||||
class LoginPage extends StatefulWidget {
|
||||
|
@ -53,9 +54,11 @@ class _LoginPageState extends State<LoginPage> {
|
|||
Spacer(),
|
||||
TextButton(
|
||||
text: "Continue",
|
||||
onClickCallback: () {
|
||||
onClickCallback: () async {
|
||||
print(controller.text);
|
||||
widget.loginManager.loginTest(controller.text);
|
||||
final authToken =
|
||||
await widget.loginManager.loginTest(controller.text);
|
||||
ConversationManager.init(authToken);
|
||||
Navigator.pushNamed(context, 'welcome/otp');
|
||||
}),
|
||||
]));
|
||||
|
|
Loading…
Reference in New Issue