25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

110 lines
2.6 KiB

  1. import 'dart:developer';
  2. import 'package:flutter/material.dart';
  3. import 'package:pusher_client/pusher_client.dart';
  4. void main() {
  5. runApp(MyApp());
  6. }
  7. class MyApp extends StatefulWidget {
  8. @override
  9. _MyAppState createState() => _MyAppState();
  10. }
  11. class _MyAppState extends State<MyApp> {
  12. PusherClient pusher;
  13. Channel channel;
  14. @override
  15. void initState() {
  16. super.initState();
  17. String token = getToken();
  18. pusher = new PusherClient(
  19. "app-key",
  20. PusherOptions(
  21. // if local on android use 10.0.2.2
  22. host: 'localhost',
  23. encrypted: false,
  24. auth: PusherAuth(
  25. 'http://example.com/broadcasting/auth',
  26. headers: {
  27. 'Authorization': 'Bearer $token',
  28. },
  29. ),
  30. ),
  31. enableLogging: true,
  32. );
  33. channel = pusher.subscribe("private-orders");
  34. pusher.onConnectionStateChange((state) {
  35. log("previousState: ${state.previousState}, currentState: ${state.currentState}");
  36. });
  37. pusher.onConnectionError((error) {
  38. log("error: ${error.message}");
  39. });
  40. channel.bind('status-update', (event) {
  41. log(event.data);
  42. });
  43. channel.bind('order-filled', (event) {
  44. log("Order Filled Event" + event.data.toString());
  45. });
  46. }
  47. String getToken() => "super-secret-token";
  48. @override
  49. Widget build(BuildContext context) {
  50. return MaterialApp(
  51. home: Scaffold(
  52. appBar: AppBar(
  53. title: const Text('Example Pusher App'),
  54. ),
  55. body: Center(
  56. child: Column(
  57. children: [
  58. ElevatedButton(
  59. child: Text('Unsubscribe Private Orders'),
  60. onPressed: () {
  61. pusher.unsubscribe('private-orders');
  62. },
  63. ),
  64. ElevatedButton(
  65. child: Text('Unbind Status Update'),
  66. onPressed: () {
  67. channel.unbind('status-update');
  68. },
  69. ),
  70. ElevatedButton(
  71. child: Text('Unbind Order Filled'),
  72. onPressed: () {
  73. channel.unbind('order-filled');
  74. },
  75. ),
  76. ElevatedButton(
  77. child: Text('Bind Status Update'),
  78. onPressed: () {
  79. channel.bind('status-update', (PusherEvent event) {
  80. log("Status Update Event" + event.data.toString());
  81. });
  82. },
  83. ),
  84. ElevatedButton(
  85. child: Text('Trigger Client Typing'),
  86. onPressed: () {
  87. channel.trigger('client-istyping', {'name': 'Bob'});
  88. },
  89. ),
  90. ],
  91. )),
  92. ),
  93. );
  94. }
  95. }