This is the official SmartFoxServer 3 API for Swift that can be used to build iOS/macOS/tvOS multiplayer clients. The Swift API is derived from the reference API implementation in Java and supports all of its features.
You can consult the latest API doc on our website: docs3.smartfoxserver.com
For questions and issue reports use the official SmartFoxServer 3 support forums: https://smartfoxserver.support/viewforum.php?f=37
import SFS3Api
class SimpleClient
{
var sfs: SmartFox
var cfg: ConfigData
init()
{
sfs = SmartFox()
cfg = ConfigData()
cfg.host = "127.0.0.1"
cfg.zone = "Playground"
sfs.addEventListener(evtType: SFSEvent.CONNECTION, delegate: EventDelegate(onConnection))
sfs.addEventListener(evtType: SFSEvent.CONNECTION_LOST, delegate: EventDelegate(onConnectionLost))
sfs.addEventListener(evtType: SFSEvent.LOGIN, delegate: EventDelegate(onLogin))
sfs.addEventListener(evtType: SFSEvent.LOGIN_ERROR, delegate: EventDelegate(onLoginError))
sfs.addEventListener(evtType: SFSEvent.ROOM_JOIN, delegate: EventDelegate(onJoinRoom))
sfs.addEventListener(evtType: SFSEvent.ROOM_JOIN_ERROR, delegate: EventDelegate(onJoinRoomError))
SFSLog.info("API Version: \(sfs.version)")
sfs.connect(cfg)
}
func onConnection(evt: ApiEvent)
{
let success = evt.params[EventParam.Success] as! Bool
if success {
SFSLog.info("Connection to \(cfg.host):\(cfg.port)")
// Login as guest user (user name assigned by the server)
sfs.send(LoginRequest(userName: ""))
} else {
SFSLog.info("Connection attempt failed!")
}
}
func onConnectionLost(evt: ApiEvent)
{
let reason = evt.params[EventParam.DisconnectionReason] as! String
SFSLog.warn("Disconnected, Reason: \(reason)")
}
func onLogin(evt: ApiEvent)
{
let user = evt.params[EventParam.User] as! BaseUser
SFSLog.info("Logged in as: \(user.name)")
// Join the main Lobby
sfs.send(JoinRoomRequest(room: "Lobby"))
}
func onLoginError(evt: ApiEvent)
{
let msg = evt.params[EventParam.ErrorMessage] as! String
SFSLog.warn("Login failed: \(msg)")
}
func onJoinRoom(evt: ApiEvent)
{
let room = evt.params[EventParam.Room] as! BaseRoom
SFSLog.info("Joined in: \(room.name)")
}
func onJoinRoomError(evt: ApiEvent)
{
let msg = evt.params[EventParam.ErrorMessage] as! String
SFSLog.warn("Join error: \(msg)")
}
}