How would I convert the following C# websocket server to a Monobehaviour class?

Hi
Bit of a C# noob
Trying to use the Websocket CSharp library
http://sta.github.io/websocket-sharp/

to create a socket server in Unity
How would I convert the following to a Monobehaviour so I can simply attach to a gameobject and run?

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 ();
}
}
}

Leave the Laputa class as it is. (Side note: I hope none of your programmers speak Spanish :wink: ). Get rid of the “Program” class and instead have a MonoBehaviour like this:

public class SocketWrapper : MonoBehaviour
{
  private Laputa wssv;

  void Start()
  {
    var wssv = new WebSocketServer ("ws://dragonsnest.far");
    wssv.AddWebSocketService<Laputa> ("/Laputa");
    wssv.Start();
  }

  void Update()
  {
     if(Input.GetKeyDown(KeyCode.Space))
        wssv.Stop();
  }
}

Thanks! Very much appreciated

Looking to get this running but only fails with :NullReferenceException: Object reference not set to an instance of an object

Post the complete error message. Otherwise noone can help you.

Also, please open a new thread next time and link a thread instead of bumping old threads…

Found the solution. Had nothing to do with this thread. Mine bad. Fixed it with an if (null) statement.