Created
September 2, 2019 00:46
-
-
Save ccfiel/e8778341a64f1dd6c87ce14f5e8f0a9a to your computer and use it in GitHub Desktop.
dart colyseus
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
import 'dart:async'; | |
import 'dart:convert'; | |
import 'dart:io'; | |
import 'package:http/http.dart' as http; | |
import 'package:localstorage/localstorage.dart'; | |
enum Platform { ios, android } | |
const TOKEN_STORAGE = "colyseus-auth-token"; | |
class Status { | |
bool status; | |
Status.fromJson(Map<String, dynamic> json) : status = json['status']; | |
} | |
class User { | |
String id; | |
String username; | |
String displayName; | |
String avatarUrl; | |
bool isAnonymous; | |
String email; | |
String lang; | |
String location; | |
String timezone; | |
String metadata; | |
List devices = []; | |
String facebookId; | |
String twitterId; | |
String googleId; | |
String gameCenterId; | |
String steamId; | |
List<String> friendIds = []; | |
List<String> blockedUserIds = []; | |
DateTime createdAt; | |
DateTime updatedAt; | |
User() { | |
id = null; | |
username = null; | |
displayName = null; | |
avatarUrl = null; | |
isAnonymous = null; | |
email = null; | |
lang = null; | |
location = null; | |
timezone = null; | |
metadata = null; | |
devices = null; | |
facebookId = null; | |
twitterId = null; | |
googleId = null; | |
gameCenterId = null; | |
steamId = null; | |
friendIds = null; | |
blockedUserIds = null; | |
createdAt = null; | |
updatedAt = null; | |
} | |
User.fromJson(Map<String, dynamic> json) | |
: id = json['_id'], | |
email = json['email'], | |
username = json['username'], | |
displayName = json['displayName'], | |
avatarUrl = json['avatarUrl'], | |
isAnonymous = json['isAnonymous'], | |
lang = json['lang'], | |
location = json['location'], | |
timezone = json['timezone'], | |
metadata = json['metadata'], | |
devices = json['devices'], | |
facebookId = json['facebookId'], | |
twitterId = json['twitterId'], | |
googleId = json['googleId'], | |
gameCenterId = json['gameCenterId'], | |
steamId = json['steamId'], | |
friendIds = json['friendIds'], | |
blockedUserIds = json['blockedUserIds']; | |
} | |
class Auth { | |
String token; | |
User user; | |
String _endpoint; | |
bool _keepOnlineInterval; | |
LocalStorage _storage; | |
Auth(String endpoint) { | |
_storage = new LocalStorage('some_key'); | |
_endpoint = endpoint.replaceAll("ws", "http"); | |
token = _storage.getItem(TOKEN_STORAGE); | |
user = User(); | |
} | |
hasToken() { | |
return token != null; | |
} | |
login({ | |
String accessToken, | |
String deviceId, | |
String platform, | |
String email, | |
String password, | |
}) async { | |
Map options = new Map(); | |
({ | |
'accessToken': accessToken, | |
'deviceId': deviceId, | |
'platform': platform, | |
'email': email, | |
'password': password | |
}) | |
.forEach((key, value) { | |
if (value != null) options[key] = value; | |
}); | |
List<String> queryParams = []; | |
options.forEach((key, value) => queryParams.add('$key=$value')); | |
if (token != null) { | |
queryParams.add('token=$token'); | |
} | |
final response = await http.post('$_endpoint/auth?${queryParams.join("&")}', | |
headers: { | |
HttpHeaders.contentTypeHeader: 'application/json; charset=utf-8' | |
}); | |
var data = json.decode(response.body); | |
token = data['token']; | |
_storage.setItem(TOKEN_STORAGE, token); | |
user = User.fromJson(data); | |
return user; | |
} | |
save() async { | |
await http.put('$_endpoint/auth', headers: { | |
HttpHeaders.contentTypeHeader: 'application/json', | |
HttpHeaders.authorizationHeader: 'Bearer ' + token | |
}, body: { | |
"username": user.username, | |
"displayName": user.displayName, | |
"avatarUrl": user.avatarUrl, | |
"lang": user.lang, | |
"location": user.location, | |
"timezone": user.timezone, | |
}); | |
return user; | |
} | |
request(String method, String url) async { | |
final headers = {HttpHeaders.authorizationHeader: 'Bearer ' + token}; | |
var response; | |
switch (method) { | |
case "POST": | |
{ | |
response = await http.post(url, headers: headers); | |
} | |
break; | |
case "GET": | |
{ | |
response = await http.get(url, headers: headers); | |
} | |
break; | |
case "PUT": | |
{ | |
response = await http.put(url, headers: headers); | |
} | |
break; | |
case "DELETE": | |
{ | |
response = await http.delete(url, headers: headers); | |
} | |
break; | |
} | |
return json.decode(response.body); | |
} | |
getFriends() async { | |
List<User> users; | |
final response = await request('GET', '$_endpoint/friends/all'); | |
for (final data in response) { | |
users.add(User.fromJson(data)); | |
} | |
return users; | |
} | |
getOnlineFriends() async { | |
List<User> users; | |
final response = await request('GET', '$_endpoint/friends/online'); | |
for (final data in response) { | |
users.add(User.fromJson(data)); | |
} | |
return users; | |
} | |
getFriendRequests() async { | |
List<User> users; | |
final response = await request('GET', '$_endpoint/friends/requests'); | |
for (final data in response) { | |
users.add(User.fromJson(data)); | |
} | |
return users; | |
} | |
sendFriendRequest(String friendId) async { | |
final response = | |
await request('POST', '$_endpoint/friends/requests?userId=$friendId'); | |
return Status.fromJson(response); | |
} | |
acceptFriendRequest(String friendId) async { | |
final response = | |
await request('PUT', '$_endpoint/friends/requests?userId=$friendId'); | |
return Status.fromJson(response); | |
} | |
declineFriendRequest(String friendId) async { | |
final response = | |
await request('DELETE', '$_endpoint/friends/requests?userId=$friendId'); | |
return Status.fromJson(response); | |
} | |
blockUser(String friendId) async { | |
final response = | |
await request('POST', '$_endpoint/friends/block?userId=$friendId'); | |
return Status.fromJson(response); | |
} | |
unblockUser(String friendId) async { | |
final response = | |
await request('PUT', '$_endpoint/friends/block?userId=$friendId'); | |
return Status.fromJson(response); | |
} | |
logout() { | |
token = null; | |
_storage.setItem(TOKEN_STORAGE, ""); | |
unregisterPingService(); | |
} | |
registerPingService(int seconds) { | |
_keepOnlineInterval = true; | |
const oneSec = const Duration(seconds: 1); | |
new Timer.periodic(oneSec * seconds, (Timer t) { | |
if (_keepOnlineInterval) { | |
request('GET', '$_endpoint/auth'); | |
} else { | |
t.cancel(); | |
} | |
}); | |
} | |
unregisterPingService() { | |
_keepOnlineInterval = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment