Skip to content

Instantly share code, notes, and snippets.

@ssebelius
Created January 13, 2015 18:34
Show Gist options
  • Save ssebelius/edf67caa58e9d44a62ae to your computer and use it in GitHub Desktop.
Save ssebelius/edf67caa58e9d44a62ae to your computer and use it in GitHub Desktop.
networkServiceLayer.RegisterListener("onlineplayers/get", OnlinePlayerCallback)
...
networkServiceLayer.RemoveListener("onlineplauers/get")
//example of how it might be used
void OnlinePlayerCallback(Hashtable data) {
//pseudocode
foreach(userObject in data) {
updatePlayerStats(playerID, status);
}
}
networkServiceLayer.SendMessage("tournament/getAllTournaments", tournamentResponseCallback)
...
void TournamentResponseCallback(Hashtable data) {
//do something with data
}
--------------------------------------------
class NetworkServiceLayer {
private Dictionary<string, Action> activeListeners = new Dictionary<string, ActioN>();
private INetworkServiceImpl service;
NetworkServiceLayer(INetworkServiceImpl impl) {
service = impl;
}
public void RegisterListener(string controller, Action<string> callback) {
service.RegisterListener(string, callback);
}
public void RemoveListener(string controller) {
service.RemoveListener(string);
}
public void SendRequest(string controller, Action<string> callback) {
service.SendRequest(controller, callback);
}
}
-----------------------
public interface INetworkServiceImpl {
public void SetupConnection();
public void SendMessage(string data, HashTable params );
public void SendMessage(string data, HashTable params, Action<Hashtable> callback);
public void RegisterListener(string metadata, Action<Hashtable> callback);
public void RemoveListener(string);
}
public class ApiaryServiceImpl: INetworkServiceImpl, MonoBehavior {
private static void URL = "http://apiary.com/...."
public void SetupConnection() { } //noop
void Start() {
}
void Awake() {
}
void SendMessage(string metadata, Action<Hashtable> callback) {
}
IEnumerator sendHttpRequest(string metadata, HashTable params, Action<Hashtable> callback) {
var request = HTTP.request("GET", ...)
yield return request.Send();
//wait for response from Apiary
Hashtable jsonResponse = (Hashtable)HTTP.JsonSerializer.Decode(message);
callback(jsonResponse);
}
}
public class WebSocketServiceImpl: INetworkServiceImpl, MonoBehavior{
private static string URL = "http://play.versusgamingnetwork.com/..." // thsi succks but you get the idea
private Router router;
private HTTP.WebSocket ws;
public HTTP.WebSocket WebSocket {
get {
return ws;
}
}
WebSocketServiceImpl() {
router = new Router();
}
void Awake() {
}
void SetupConnection() {
StartCoroutine(connect(URL))
}
void SendMessage(string metadata, HashTable params, Action<Hashtable> callback) {
//register register the callback with the appropriate metadata
Websocket.Send(metadata);
}
void IEnumerator connect(server URL) {
//connect to Websocket and handle messages
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment