[Resolved]Not Recieving a Response

Options
Dilvid
edited October 2011 in DotNet
Hi there,

I made the simple chat server/chat client from the website and it all works fine (after my school boy XML mistake). The next step I wanted to take was replace the .NET client with a Unity3D client. I'm not using the plugin which is available as I like to know what it is doing under the hood so writing it all myself lol.

Anyway, I copied the code with a few minor tweaks from my .NET client but it doesn't completly work. According to some debug messages I wrote it is connecting however as soon as I send any data from the Unity3d client it seems to disconnect.

Can someone glance at my code at tell me if I'm being dumb?
using UnityEngine;
using System.Collections;

using ExitGames.Client.Photon;
using System.Collections.Generic;

public class ChatClient : MonoBehaviour, IPhotonPeerListener
{
    private bool connected;
    public string strEditedText = "";
    public string strRecievedText = "";

    public PhotonPeer peer;

	void Start () 
    {
        var client = this;
        peer = new PhotonPeer(client, true);

        client.connected = false;
        peer.Connect("127.0.0.1:4530", "ChatServer");
        while (!client.connected)
        {
            peer.Service();
        }
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    private void OnGUI()
    {
        strEditedText = GUI.TextField(new Rect(10, 10, 200, 20), strEditedText);
        if(GUI.Button(new Rect(230, 10, 130, 20), "Send Message"))
        {
            var parameters = new Dictionary<byte, object> { { 1, strEditedText } };
            peer.OpCustom(1, parameters, true);
            strEditedText = string.Empty;
        }
        GUI.TextArea(new Rect(10, 40, 450, 500), strRecievedText);
    }

    public void WriteLine(string Message)
    {
        strRecievedText += string.Format("{0}{1}", System.Environment.NewLine, Message);
    }

    public void DebugReturn(DebugLevel level, string message)
    {
        Debug.Log(string.Format("{0}: {1}", level.ToString(), message));
        WriteLine(string.Format("{0}: {1}", level.ToString(), message));
    }

    public void OnEvent(EventData eventData)
    {
        WriteLine(string.Format("Event: {0}", eventData.Code));

        if(eventData.Code == 1)
        {
            WriteLine(string.Format("Chat: {0}", eventData.Parameters[1]));
        }
    }

    public void OnOperationResponse(OperationResponse operationResponse)
    {
        WriteLine(string.Format("Response: {0}", operationResponse.OperationCode));
    }

    public void OnStatusChanged(StatusCode statusCode)
    {
        if (statusCode == StatusCode.Connect)
        {
            this.connected = true;
        }
        else
            WriteLine(string.Format("Status: {0}", statusCode));
    }
}

Comments

  • Well I figured it out, minor schoolboy error of writing application name as 'ChatSever' instead of 'ChatServer' and also seems you need to perform a sort of keep alive function in the Update method in Unity. Anyways I can now send messages back and forth rather well, the issue now is occasionally I am being disconnected.

    Gathering it is along the lines of too much data waiting to be sent without a response, reason I think this is due to that if I open my Unity Chat Client and then open my .NET client which both connect to the same 'ChatServer' and do the following then I can replicate the crash.
    • Select Focus on the .NET chat client (Unity Client looses Focus)
    • Send more that one message to the server
    When I then bring focus back to the UnityClient it then crashes and disconnects me. Without looking too much into it I presume no events are handled whent he Unity client doesn't have focus so hence a backlog of data which then as no response given the server disconnects you. Will look into it properly tonight when I have a bit more time but if anyone has any other ideas or knows what is going on please drop a mesage.
  • dreamora
    Options
    Its not keep alive but telling the networking to run at all.

    Optimally you would not use the DotNET SDK but actually the Unity SDK which has all the relevant things in to work with unity (cause the service thread must not call into the other code asyncronous or unity will crash instantly. Unity is not thread nor callback save!)
  • Tobias
    Options
    Is it a disconnect or a true crash?
    If it's just a disconnect, make sure to use Unity's RunInBackground.
    http://unity3d.com/support/documentatio ... round.html

    By the way: Service() also does a keep alive, where necessary. So you don't have to implement anything like that.
  • Thanks Tobias, That was my issue. I didn't have the #.Service(); being called in my update statement so it was just being disconnected.
  • Tobias
    Options
    The idea of Service() is to do the client's work when you assign it some time in your main loop (when you expect this work to be done within your game's logic).
    This can be annoying in some cases ;)

    Keep it in mind, when you ever have to pause your game's thread for a while. Then you have to call Service() or SendOutgoingCommands() by some other means.