Skip to content

Instantly share code, notes, and snippets.

@jcspencer
Last active December 10, 2015 13:18
Show Gist options
  • Save jcspencer/4439820 to your computer and use it in GitHub Desktop.
Save jcspencer/4439820 to your computer and use it in GitHub Desktop.
Ouya Multiplayer API demo
//Create
OuyaLobby lobby = new OuyaLobby(max_players, port, region);
//Connect
OuyaLobby lobby = new OuyaLobby(host, port);
lobby.connect(new Callback() {
@Override
public void onMessage(JSONObject json, Acknowledge ack) {
try {
System.out.println("Server said:" + json.toString(2));
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onMessage(String data, Acknowledge ack) {
System.out.println("Server said: " + data);
}
@Override
public void onError(ouyaIOException ouyaIOException) {
System.out.println("an Error occured");
ouyaIOException.printStackTrace();
}
@Override
public void onDisconnect() {
System.out.println("Connection terminated.");
}
@Override
public void onConnect() {
System.out.println("Connection established");
}
@Override
public void on(String event, Acknowledge ack, Object... args) {
System.out.println("Server triggered event '" + event + "'");
}
});
//text
lobby.send("Hello Server!");
//json
JSONObject object=new JSONObject();
object.put("name", "James Spencer");
lobby.send(object);
var net = require('net');
var Lobby = require('./lobby.js')
function createLobby(players, protocol, net) {
var lobby = new Lobby(players, protocol, net, port, function(err, host, port) {
console.log('Lobby listening on host ' + host + ':' + port);
});
lobby.on('connect', function(from, event) {
console.log(from.playerName + ' connected at ' + event.time.toString());
});
lobby.on('disconnect', function(from, event) {
console.log(from.playerName + ' disconnected at ' + event.time.toString() + ' because ' + event.reason);
});
lobby.on('action', function(fromgame, from, event) {
if (fromgame) { // If non-player action
console.log(from.sender + ' emitted ' + event);
} else {
console.log(from.playerName + ' emitted ' + event);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment