簡體   English   中英

使用不同用戶 Unity-Smartfoxserver 連接服務器的問題

[英]Problem with connecting to server using different users Unity-Smartfoxserver

我正在制作一個簡單的 memory 游戲。 我已經使游戲與 smartfoxserver 一起工作。 但是當我嘗試構建另一台機器並讓它們同時運行時,一個玩家會在另一個玩家登錄時退出。你們能幫我解決這個問題嗎? 這是客戶端上的代碼。 也就是一旦游戲開始,兩台機器有什么方法可以相互連接。 例如顯示從 Player1 到 Player2 的分數。 謝謝你。

using Sfs2X;
using Sfs2X.Core;
using Sfs2X.Entities.Data;
using Sfs2X.Requests;
using Sfs2X.Util;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices.ComTypes;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using Sfs2X.Requests.MMO;
 public class GameController : MonoBehaviour
{
public string defaultHost = "127.0.0.1";
public int defaultTcpport = 8888;
public int defaultWsport = 8080;
public string Zonename = "BasicExamples";
public string Username = "guest";
public string Roomname = "The Lobby";

private SmartFox sfs;

void Awake()
{
    SourceSprites = Resources.LoadAll<Sprite>("Sprite/GameImages");
}
void Start()
{
    Login_Click();
    TotalGuess = btnlist.Count / 2;
    GetButton();
    AddListener();
    AddSprites();
    shuffle(GameSprite);
   }
 public void Login_Click()
   {
    if (sfs == null || !sfs.IsConnected)
    {
        sfs = new SmartFox();
        sfs.ThreadSafeMode = true;
        sfs.AddEventListener(SFSEvent.CONNECTION, OnConnection);
        sfs.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
        sfs.AddEventListener(SFSEvent.LOGIN, OnLogin);
        sfs.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginError);
        sfs.AddEventListener(SFSEvent.ROOM_JOIN, OnJoinRoom);
        sfs.AddEventListener(SFSEvent.ROOM_JOIN_ERROR, OnJoinRoomError);
        sfs.AddEventListener(SFSEvent.EXTENSION_RESPONSE, GetResult);


        ConfigData cfg = new ConfigData();
        cfg.Host = defaultHost;
        cfg.Port = defaultTcpport;
        cfg.Zone = "BasicExamples";
        cfg.Debug = true;
        Debug.LogError("defaultHost " + defaultHost);
        Debug.LogError("defaultTcpport " + defaultTcpport);
        sfs.Connect(cfg);
    }
}
void OnLogin(BaseEvent evt)
{
    Debug.Log("Login Success");
    sfs.Send(new JoinRoomRequest("The Lobby"));
}
  void OnJoinRoom(BaseEvent evt)
{

    Debug.Log("Joined Room"+ evt.Params["room"]);
}
void OnJoinRoomError(BaseEvent evt)
{
    Debug.Log("Join Room Error" + evt.Params["errorMessage"]);
}
void OnLoginError(BaseEvent evt)
{
    Debug.Log("Login Error"+ evt.Params["errorMessage"]);
}
void OnConnection(BaseEvent evt)
{
    if ((bool)evt.Params["success"])
    {
        Debug.Log("Connection Success");
        sfs.Send(new LoginRequest(Username, "", Zonename));
    }
    else
    {
        Debug.Log("Connection Error");
    }
}
void OnConnectionLost(BaseEvent evt)
{

}

您的問題是,當您執行 LoginRequest 時,您的所有客戶都具有相同的用戶名。 SFS 會自動斷開具有相同用戶名的其他用戶。 您必須為所有客戶創建一個唯一的用戶名,以便他們可以連接在一起。 最簡單的方法是使用設備 ID 作為用戶名。

sfs.Send(new LoginRequest(SystemInfo.deviceUniqueIdentifier, "", Zonename));

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM