---
# System prepended metadata

title: connectStatus
tags: [so]

---

---
title: 'connectStatus'
disqus: hackmd
tags: 'so'
---
## **Track Connection Status**
How to grasp the actual status of connection from Client.
      	
After a player logs in, he can see the current connection status in the lower right corner.
     	
![](https://i.imgur.com/lhwIJcL.jpg)  
  	
```
C#
using UnityEngine;
using System.Collections;
 
public class Sample_ConnectionState : MonoBehaviour {
	
	/*******************************************************************
	*   Copyright © 2022 SPKITA Game Cloud. All Rights Reserved
	*  
	*******************************************************************/
	
	//Enter the gguid shown in the developer backstage here
	private const string gguid = "";	
	
	//Enter the verification shown in the developer backstage here
	byte[] certificate = {};
	
	//Declare CloudGame and this Class will be in charge of the communication with Cloud Server
	public CloudGame ag = null;
	
	string userAcc; //User’s account
	string userPwd; //User’s password
 
	void Start()
	{
        CloudSystem.UnityEnvironment();
        CloudSystem.ServerProvider("sgc-api-us.spkita.com"); // set the server
	}
	
	void OnApplicationQuit()
	{
    	//When the program is closed, the user’s account will not be logged out. Here we can add a mechanism that allows automatic logging out.
    	if(ag!=null) ag.Dispose();  	
	}   	
	
	public void LoginBtnClick()
	{
    	userAcc = login_input_acc.text;
    	userPwd = login_input_pwd.text;
    	
    	//the parameters needed when logging in, which are (account, password, gguid, certificate)
    	ag = new CloudGame(userAcc, userPwd, gguid, certificate);
 
    	//Designate the way of management and start the detection on whether the connection is successful
    	ag.onCompletion += CloudLaunch;  	
 
    	//When there is a change in connection status, OnStateChange will notify the program
    	ag.onStateChanged += OnStateChanged;	
    	
        ag.UnityLaunch();
 
	}
    	
	/*******************************************************************
	//  After CloudGame finishes its execution, in CallBack there will be a message about whether the login is successful
	//  When the code is 0, the login succeeds and when the code is not 0, the login fails
	//  Error Code	Refer the errors in login to Error Code.
	// 
	*******************************************************************/
	
	void CloudLaunch(int code, CloudGame game)
	{
    	if (code == 0)
    	{
        	Debug.Log("Login Success! My poid : " + ag.poid +
        	" NickName : " + ag.nickname);
    	}
    	else
    	{
            Debug.LogWarning("Login Fail! Code : " + code);
    	}
	}
	
	/*******************************************************************
	//  Triggered when the connection status with the cloud game server changes
	//  Refer the detailed codes to API item models=>CloudGame=>Event
	*******************************************************************/
      	
	void OnStateChanged(int state, int code, CloudGame game)
	{
    	//Debug.Log("StateChanged = " + state + " , Code = " + code);
        lobby_State.text = "State : " + state;
 
    	if (state >= 900)
    	{
            Debug.LogError("Game Disconnect!");
    	}
	}  
	
}
```
