简体   繁体   中英

Problem with connecting to server using different users Unity-Smartfoxserver

I am making a simple memory game. I have already made the game work with 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. 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. SFS automatically disconnect other users with the same username. 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.

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

hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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