@@ -25,6 +25,7 @@ class _DataChannelSampleState extends State<DataChannelSample> {
2525 var _text = '' ;
2626 // ignore: unused_element
2727 _DataChannelSampleState ();
28+ bool _waitAccept = false ;
2829
2930 @override
3031 initState () {
@@ -39,6 +40,55 @@ class _DataChannelSampleState extends State<DataChannelSample> {
3940 _timer? .cancel ();
4041 }
4142
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+
4292 void _connect (BuildContext context) async {
4393 _signaling ?? = Signaling (widget.host, context)..connect ();
4494
@@ -65,33 +115,54 @@ class _DataChannelSampleState extends State<DataChannelSample> {
65115 }
66116 };
67117
68- _signaling? .onCallStateChange = (Session session, CallState state) {
118+ _signaling? .onCallStateChange = (Session session, CallState state) async {
69119 switch (state) {
70120 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 ;
80126 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 );
91131 }
132+ setState (() {
133+ _inCalling = false ;
134+ });
135+ _timer? .cancel ();
136+ _dataChannel = null ;
137+ _inCalling = false ;
138+ _session = null ;
139+ _text = '' ;
140+ break ;
92141 case CallState .CallStateInvite :
142+ _waitAccept = true ;
143+ _showInvateDialog ();
144+ break ;
93145 case CallState .CallStateConnected :
146+ if (_waitAccept) {
147+ _waitAccept = false ;
148+ Navigator .of (context).pop (false );
149+ }
150+ setState (() {
151+ _inCalling = true ;
152+ });
153+ break ;
94154 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 ;
95166 }
96167 };
97168
@@ -117,6 +188,18 @@ class _DataChannelSampleState extends State<DataChannelSample> {
117188 }
118189 }
119190
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+
120203 _hangUp () {
121204 _signaling? .bye (_session! .sid);
122205 }
0 commit comments