简体   繁体   English

使用不同用户 Unity-Smartfoxserver 连接服务器的问题

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

I am making a simple memory game.我正在制作一个简单的 memory 游戏。 I have already made the game work with smartfoxserver.我已经使游戏与 smartfoxserver 一起工作。 But when I tried to build another machine and let them run simultaneously, one player would be log out when another log in. Could you guys help me with this one.但是当我尝试构建另一台机器并让它们同时运行时,一个玩家会在另一个玩家登录时退出。你们能帮我解决这个问题吗? Here is the code on the client.这是客户端上的代码。 Also is once the game start is there any way for the two machine to connect to eachother.也就是一旦游戏开始,两台机器有什么方法可以相互连接。 For example showing the score from Player1 to Player2.例如显示从 Player1 到 Player2 的分数。 Thank you.谢谢你。

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)
{

}

Your problem is that all of your clients have the same username when you do LoginRequest.您的问题是,当您执行 LoginRequest 时,您的所有客户都具有相同的用户名。 SFS automatically disconnect other users with the same username. SFS 会自动断开具有相同用户名的其他用户。 You must create a unique username for all of your clients to they can connect together.您必须为所有客户创建一个唯一的用户名,以便他们可以连接在一起。 The simplest way to do this is to use the device id as a username.最简单的方法是使用设备 ID 作为用户名。

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

hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM