-
Notifications
You must be signed in to change notification settings - Fork 5
Tetris Client
The tetris client interfaces with Beam's Tetris servers, allowing your app to send and receive updates to control data.
To connect to a Tetris server, first retrieve data that you will need in order to establish connection. This can be done with a method in the TetrisRoutes
class.
After retrieving the connection data, you will be able to join a channel using one of two methods, depending on whether there is a user authenticated with the BeamSession
class.
let channelId = 50772
let tetrisClient = TetrisClient(delegate: self)
BeamClient.sharedClient.tetris.getTetrisDataByChannel(id) { (data, error) -> Void in
guard let data = data, address = data.address else {
return
}
if let key = data.key, userId = data.userId {
// there is an authenticated user, and they can interact with the controls
tetrisClient.connect(url: address, channelId: id, key: key, userId: userId)
} else {
// there is no locally authenticated user, and they cannot interact with the controls
tetrisClient.connect(url: address, channelId: id)
}
}
The delegate methods will be passing your app data about the connection you have with the Tetris server in the meantime, which you should listen to and use to update your app accordingly.
The tetrisDidConnect:
delegate method will be fired when a connection is made to a Tetris server. At this point in time, packets will start being sent to the client by the Tetris server. You will need to listen for a HandshakeAcknowledgmentPacket
, which will indicate that you are then able to send packets to the server.
func tetrisDidConnect() {
print("connected to tetris")
}
The tetrisDidDisconnect:
delegate method will be fired when your connection with the Tetris server has been disrupted, whether by accident or on purpose.
func tetrisDidDisconnect() {
print("disconnected from tetris")
}
The tetrisChangedState:state:
delegate method will be fired when the control state has changed. The control state dictates how controls should be displayed at a given point in time. Once this method has been fired, you can act on this by using the applicable blueprint configurations found in each control.
func tetrisChangedState(state: String) {
print("changed state to \(state)")
for control in controls {
for configuration in control.blueprint.configurations
where configuration.grid == .Small && configuration.state == state {
// update display accordingly
}
}
}
The tetrisReceivedPacket:packet:
delegate method will be fired when a packet has been received from the Tetris server. This packet has already been parsed and interpreted by the API client, allowing you to use the packet data effortlessly.
func tetrisReceivedPacket(packet: TetrisPacket) {
if let _ = packet as? HandshakeAcknowledgmentPacket {
// packets can be sent now
print("handshake acknowledgment received")
}
}