platform/packages/websocket/test/controller/common.dart

40 lines
1,013 B
Dart
Raw Normal View History

2021-05-15 07:19:35 +00:00
import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel3_websocket/server.dart';
2023-06-10 04:10:21 +00:00
import 'package:quiver/core.dart';
2016-12-23 20:57:46 +00:00
class Game {
2021-04-26 00:47:32 +00:00
final String? playerOne, playerTwo;
2016-12-23 20:57:46 +00:00
const Game({this.playerOne, this.playerTwo});
2016-12-24 01:45:52 +00:00
2021-02-21 02:47:23 +00:00
factory Game.fromJson(Map data) => Game(
2018-07-10 16:54:55 +00:00
playerOne: data['playerOne'].toString(),
playerTwo: data['playerTwo'].toString());
2016-12-24 01:45:52 +00:00
2018-11-04 02:17:33 +00:00
Map<String, dynamic> toJson() {
return {'playerOne': playerOne, 'playerTwo': playerTwo};
}
2016-12-24 01:45:52 +00:00
@override
bool operator ==(other) =>
other is Game &&
other.playerOne == playerOne &&
other.playerTwo == playerTwo;
2023-06-10 04:10:21 +00:00
@override
int get hashCode => hash2(playerOne, playerTwo);
2016-12-23 20:57:46 +00:00
}
2021-02-21 02:47:23 +00:00
const Game johnVsBob = Game(playerOne: 'John', playerTwo: 'Bob');
2016-12-24 01:45:52 +00:00
2016-12-23 20:57:46 +00:00
@Expose('/game')
class GameController extends WebSocketController {
2023-12-24 01:52:57 +00:00
GameController(super.ws);
2017-10-19 22:26:59 +00:00
2016-12-23 20:57:46 +00:00
@ExposeWs('search')
2021-04-10 15:12:43 +00:00
dynamic search(WebSocketContext socket) async {
2016-12-24 01:45:52 +00:00
print('User is searching for a game...');
2018-07-10 16:54:55 +00:00
socket.send('searched', johnVsBob);
2016-12-23 20:57:46 +00:00
}
2016-12-24 01:45:52 +00:00
}