如何更改收到的数据类型?

时间:2017-12-03 14:51:16

标签: c# unity3d signalr

Unity中有我的客户端代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using SignalR.Client._20.Hubs;
using SignalR.Client._20.Transports;
using Newtonsoft.Json.Linq;

public class SignalRHelper : MonoBehaviour {
    public string signalRUrl = "http://localhost:52527";

    public Text text;
    public InputField message;
    public GameObject testObj;


    private string _resault;


    private HubConnection _hubConnection = null;
    private IHubProxy _hubProxy;


    private IEnumerator Start()
    {
        Debug.Log("start()");
        yield return new WaitForSeconds(1);
        Debug.Log("start() 1 second");

        StartSignalR();
    }

    void StartSignalR()
    {
        Debug.Log("StartSignalR");
        if (_hubConnection == null)
        {
            _hubConnection = new HubConnection(signalRUrl);
            Debug.Log(signalRUrl);
            _hubConnection.Error += HubConnection_Error;

            _hubProxy = _hubConnection.CreateProxy("ChatHub");

            Subscription subscription = _hubProxy.Subscribe("synx");
            subscription.Data += ChangePosition;
            subscription = _hubProxy.Subscribe("broadcastMessage");
            subscription.Data += Chat;

            Debug.Log("_hubConnection.Start();");
            _hubConnection.Start();
        }
        else
        {
            Debug.Log("Singnal already connected...");
        }

        //Пришёл ответ с сервера (got answer from server)

    }
    private void ChangePosition(object[] obj)
    {
        Debug.Log("Prishlo");
        Debug.Log(obj[0] + "," + obj[1] + "," + obj[2]);

    }
    private void Chat(object[] obj)
    {
        Debug.Log(obj[1].ToString());
        _resault += obj[0] + ":" + obj[1] + System.Environment.NewLine;
    }

    //
    // Попробовать изменить на потоки
    //
    //


    private void Update()
    {
        text.text = _resault;
        if (Input.GetKeyUp(KeyCode.E))
        {
            Debug.Log("hubProxy of cube: " + _hubProxy);
            _hubProxy.Invoke("Synx", testObj.transform.position.x,       testObj.transform.position.y, testObj.transform.position.z);
        }
    }
    private void HubConnection_Error(System.Exception obj)
    {
        Debug.Log("Hub Error = " + obj.Message +System.Environment.NewLine +     obj.InnerException +
        System.Environment.NewLine + obj.Data +
        System.Environment.NewLine + obj.StackTrace +
        System.Environment.NewLine + obj.TargetSite);
    }

    private void HubConnection_Closed()
    {
    Debug.Log("hubConnection_Closed()");
    }

    public void SendMessage()
    {
        _hubProxy.Invoke("Send", "UnityEpta", message.text);
    }

    //Если вышли с приложения нужно остановить Connection

    private void OnApplicationQuit()
    {
        Debug.Log("OnApplicationQuit " + Time.time + "seconds");
        _hubConnection.Error -= HubConnection_Error;
        _hubConnection.

它在服务器中:

using System;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace SignalRChat
{
    public class ChatHub : Hub
    {
        public void Send(string name, string message)
        {
            // Call the broadcastMessage method to update clients.
            Clients.All.broadcastMessage(name, message);
        }
        public void Synx(Stock stock)
        {
            Clients.All.synx(stock);
        }
    }
    public class Stock
    {
        public float x { get; set; }
        public float y { get; set; }
        public float z { get; set; }
    }
}

我在'object'类型中接收testObj的位置,但我不能用'object'类型改变位置。我试图从'对象'改为'浮动',但没有任何事情发生。 然后我将'private void ChangePosition(object [] obj)'替换为'private void ChangePosition(float [] obj)',我得到错误,只有'object'类型.Method'Convert.ChangeType'don'工作。如何将接收数据更改为float或如何更改testObj的位置 在另一种方式?

1 个答案:

答案 0 :(得分:0)

方法签名必须匹配!

(表示方法名称以及参数的数量和类型)

public void Synx(Stock stock) 

不符合:

_hubProxy.Invoke("Synx", testObj.transform.position.x,       testObj.transform.position.y, testObj.transform.position.z);

为什么不添加mycontracts等其他libray并在客户端和服务器项目中引用它。

比你可以

_hubProxy.Invoke("Synx", new Stock({...);