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