简体   繁体   English

将C#Websocket示例转换为Unity MonoBehaviour

[英]Convert C# Websocket example to Unity MonoBehaviour

Bit of a C# noob. C#菜鸟的位。
Trying to use the Websocket CSharp library http://sta.github.io/websocket-sharp/ to create a socket server in Unity. 尝试使用Websocket CSharp库http://sta.github.io/websocket-sharp/在Unity中创建套接字服务器。
How would I convert the following to a Monobehaviour so I can simply attach to a gameobject and run? 我如何将以下内容转换为Monobehaviour,以便可以简单地附加到游戏对象并运行?
I know obviously I am extending Monobehaviour and I am dealing with Awake , Start and Update methods, but a bit fuzzy when it comes to nested classes, namespaces , what code in the following I would put in my Start method, and whether I should keep everything in just one .cs file or break it up, one file per class. 我显然知道我正在扩展Monobehaviour,并且正在处理AwakeStartUpdate方法,但是当涉及到嵌套类,名称空间,我将在Start方法中放入以下代码以及是否应该保留时,有点模糊所有内容都放在一个.cs文件中或将其分解,每个类一个文件。

using System;
using WebSocketSharp;
using WebSocketSharp.Server;

namespace Example
{
  public class Laputa : WebSocketBehavior
  {
    protected override void OnMessage (MessageEventArgs e)
    {
      var msg = e.Data == "BALUS"
      ? "I've been balused already..."
      : "I'm not available now.";

      Send (msg);
    }
  }

  public class Program
  {
    public static void Main (string[] args)
    {
      var wssv = new WebSocketServer ("ws://dragonsnest.far");
      wssv.AddWebSocketService<Laputa> ("/Laputa");
      wssv.Start ();
      Console.ReadKey (true);
      wssv.Stop ();
    }
  }
}


using WebSocketSharp;
using WebSocketSharp.Server;

namespace Example
{
  public class Laputa : WebSocketBehavior
  {
    protected override void OnMessage (MessageEventArgs e)
    {
      var msg = e.Data == "BALUS"
      ? "I've been balused already..."
      : "I'm not available now.";

      Send (msg);
    }
  }

  public class Program
  {
    public static void Main (string[] args)
    {
      var wssv = new WebSocketServer ("ws://dragonsnest.far");
      wssv.AddWebSocketService<Laputa> ("/Laputa");
      wssv.Start ();
      Console.ReadKey (true);
      wssv.Stop ();
    }
  }
}

First off, I'd declare the wssv variable as a class variable, at the top of your MonoBehavior. 首先,我将在MonoBehavior的顶部将wssv变量声明为类变量。

Initialization (constructing wssv, up through calling Start()) goes in Start(). 初始化(通过调用Start()构建wssv)进入Start()。

Input checking (which would replace your Console.ReadKey) goes in Update(). 输入检查(将替换Console.ReadKey)在Update()中。 You'll want to check for Input.GetKeyDown() if you want to stop when (ex. Escape) key is pressed, then call Stop on the wssv object. 如果要在按下(例如Escape)键时停止,则需要检查Input.GetKeyDown(),然后在wssv对象上调用Stop。

Generally in Unity3d we keep separate classes in separate files, even when they're short. 通常,在Unity3d中,即使它们很短,我们也会在单独的文件中保留单独的类。 So I'd recommend moving the Laputa class into a file of its own (and it shouldn't be a MonoBehavior). 因此,我建议将Laputa类移至其自己的文件中(并且不应将其作为MonoBehavior)。 After that you're MonoBehavior file will have the variable declaration, Start() and Update(). 之后,您的MonoBehavior文件将具有变量声明Start()和Update()。 Very clean. 很干净。

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

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