-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41ccc3f
commit 3f9ca2d
Showing
5 changed files
with
124 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:web_socket_channel/web_socket_channel.dart'; | ||
import 'package:wox/entity.dart'; | ||
|
||
class WoxWebsocket { | ||
WebSocketChannel? channel; | ||
final Uri uri; | ||
|
||
Function onMessageReceived; | ||
|
||
WoxWebsocket(this.uri, {required this.onMessageReceived}); | ||
|
||
void connect() { | ||
channel?.sink.close(); | ||
channel = null; | ||
|
||
channel = WebSocketChannel.connect(uri); | ||
channel!.stream.listen( | ||
(event) { | ||
onMessageReceived(event); | ||
}, | ||
onDone: () { | ||
_reconnect(); | ||
}, | ||
); | ||
} | ||
|
||
void sendMessage(WebsocketMsg msg) { | ||
channel?.sink.add(jsonEncode(msg)); | ||
} | ||
|
||
void _reconnect() { | ||
Future.delayed(const Duration(seconds: 1), () { | ||
print("Attempting to reconnect to WebSocket..."); | ||
connect(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,4 +31,4 @@ SPEC CHECKSUMS: | |
|
||
PODFILE CHECKSUM: 16208599a12443d53889ba2270a4985981cfb204 | ||
|
||
COCOAPODS: 1.11.3 | ||
COCOAPODS: 1.12.1 |