@@ -25,6 +25,7 @@ class _DataChannelSampleState extends State<DataChannelSample> {
25
25
var _text = '' ;
26
26
// ignore: unused_element
27
27
_DataChannelSampleState ();
28
+ bool _waitAccept = false ;
28
29
29
30
@override
30
31
initState () {
@@ -39,6 +40,55 @@ class _DataChannelSampleState extends State<DataChannelSample> {
39
40
_timer? .cancel ();
40
41
}
41
42
43
+ Future <bool ?> _showAcceptDialog () {
44
+ return showDialog <bool ?>(
45
+ context: context,
46
+ builder: (context) {
47
+ return AlertDialog (
48
+ title: Text ("title" ),
49
+ content: Text ("accept?" ),
50
+ actions: < Widget > [
51
+ MaterialButton (
52
+ child: Text (
53
+ 'Reject' ,
54
+ style: TextStyle (color: Colors .red),
55
+ ),
56
+ onPressed: () => Navigator .of (context).pop (false ),
57
+ ),
58
+ MaterialButton (
59
+ child: Text (
60
+ 'Accept' ,
61
+ style: TextStyle (color: Colors .green),
62
+ ),
63
+ onPressed: () => Navigator .of (context).pop (true ),
64
+ ),
65
+ ],
66
+ );
67
+ },
68
+ );
69
+ }
70
+
71
+ Future <bool ?> _showInvateDialog () {
72
+ return showDialog <bool ?>(
73
+ context: context,
74
+ builder: (context) {
75
+ return AlertDialog (
76
+ title: Text ("title" ),
77
+ content: Text ("waiting" ),
78
+ actions: < Widget > [
79
+ TextButton (
80
+ child: Text ("cancel" ),
81
+ onPressed: () {
82
+ Navigator .of (context).pop (false );
83
+ _hangUp ();
84
+ },
85
+ ),
86
+ ],
87
+ );
88
+ },
89
+ );
90
+ }
91
+
42
92
void _connect (BuildContext context) async {
43
93
_signaling ?? = Signaling (widget.host, context)..connect ();
44
94
@@ -65,33 +115,54 @@ class _DataChannelSampleState extends State<DataChannelSample> {
65
115
}
66
116
};
67
117
68
- _signaling? .onCallStateChange = (Session session, CallState state) {
118
+ _signaling? .onCallStateChange = (Session session, CallState state) async {
69
119
switch (state) {
70
120
case CallState .CallStateNew :
71
- {
72
- setState (() {
73
- _session = session;
74
- _inCalling = true ;
75
- });
76
- _timer =
77
- Timer .periodic (Duration (seconds: 1 ), _handleDataChannelTest);
78
- break ;
79
- }
121
+ setState (() {
122
+ _session = session;
123
+ });
124
+ _timer = Timer .periodic (Duration (seconds: 1 ), _handleDataChannelTest);
125
+ break ;
80
126
case CallState .CallStateBye :
81
- {
82
- setState (() {
83
- _inCalling = false ;
84
- });
85
- _timer? .cancel ();
86
- _dataChannel = null ;
87
- _inCalling = false ;
88
- _session = null ;
89
- _text = '' ;
90
- break ;
127
+ if (_waitAccept) {
128
+ print ('peer reject' );
129
+ _waitAccept = false ;
130
+ Navigator .of (context).pop (false );
91
131
}
132
+ setState (() {
133
+ _inCalling = false ;
134
+ });
135
+ _timer? .cancel ();
136
+ _dataChannel = null ;
137
+ _inCalling = false ;
138
+ _session = null ;
139
+ _text = '' ;
140
+ break ;
92
141
case CallState .CallStateInvite :
142
+ _waitAccept = true ;
143
+ _showInvateDialog ();
144
+ break ;
93
145
case CallState .CallStateConnected :
146
+ if (_waitAccept) {
147
+ _waitAccept = false ;
148
+ Navigator .of (context).pop (false );
149
+ }
150
+ setState (() {
151
+ _inCalling = true ;
152
+ });
153
+ break ;
94
154
case CallState .CallStateRinging :
155
+ bool ? accept = await _showAcceptDialog ();
156
+ if (accept! ) {
157
+ _accept ();
158
+ setState (() {
159
+ _inCalling = true ;
160
+ });
161
+ } else {
162
+ _reject ();
163
+ }
164
+
165
+ break ;
95
166
}
96
167
};
97
168
@@ -117,6 +188,18 @@ class _DataChannelSampleState extends State<DataChannelSample> {
117
188
}
118
189
}
119
190
191
+ _accept () {
192
+ if (_session != null ) {
193
+ _signaling? .accept (_session! .sid, 'data' );
194
+ }
195
+ }
196
+
197
+ _reject () {
198
+ if (_session != null ) {
199
+ _signaling? .reject (_session! .sid);
200
+ }
201
+ }
202
+
120
203
_hangUp () {
121
204
_signaling? .bye (_session! .sid);
122
205
}
0 commit comments