Namespace: Nonoba.api
Language: ActionScript 3
An instance of this class is returned when the Multiplayer API is initialized successfully.
You this instance of connection to send and receive data to and from the server. Data is send using the Send method, and data is received by listening to the MessageEvent.MESSAGE event. Making an instance of the connection class
Lets initialize the multiplayer API and send a message to the server as an example.
var connection:Connection = NonobaAPI.MakeMultiplayer(stage);
connection.Send("hello");
Send(message:Message):void
Sends a message to the server using a message object
Send(type:String, [...args:*]):void
Sends a message to the server by parsing the type and data directly to the Send method.
ConnectionEvent.INIT
Triggered when the multiplayer system is done initializing, has connected to the server and the Nonoba overlay has unloaded.
ConnectionEvent.DISCONNECT
Triggered if the multiplayer system fails to initialize, connect or the connection is lost during the game.
MessageEvent.MESSAGE
Triggered when a message is received from the server.
public function Send(message:Message):void
Send a message to the server by parsing a Message instance.
public function Send(type:String, [...args:*]):void
Send a message to the server by parsing arguments directly to the Send method.
The first argument must be the type of the message parsed as a string.
Hereafter data can be attached to the message by just adding them to the method call.
//Initialize the multiplayer API (You can only do this once)
var connection:Connection = NonobaAPI.MakeMultiplayer(stage);
// Create a message
var m = new Message("testmessage");
m.Add("Data 1");
m.Add(2)
m.Add(true)
connection.Send(m);
Example 2
This example does the same as the above but call the Send method with type and data directly.
//Initialize the multiplayer API (You can only do this once)
var connection:Connection = NonobaAPI.MakeMultiplayer(stage);
//Send the message
connection.Send("testmessage", "Data 1", 2, true);
The below example shows a full document class for a FLA file which initializes the multiplayer API, sends a message and attach to all events
package {
import flash.display.MovieClip
import flash.display.BlendMode
import Nonoba.api.*
public class ExampleGame extends MovieClip{
private var connection:Connection
function ExampleGame(){
stop();
//Init MP API
connection = NonobaAPI.MakeMultiplayer(stage);
//Handle message
connection.addEventListener(MessageEvent.MESSAGE, onMessage)
//Handle INIt
connection.addEventListener(ConnectionEvent.INIT, onInit)
//Handle error state
connection.addEventListener(ConnectionEvent.DISCONNECT, onDisconnect)
//Send message to the server
connection.Send("TestMessage")
}
private function onMessage(e:Object):void{
var message:Object = e.message;
trace("Got message " + e)
}
private function onInit(e:Object):void{
trace("Everything is done loading and connecting")
}
private function onDisconnect(e:Object):void{
trace("An error occured")
}
}
}