static void Main(string[] args) { try { SharpClient client = new SharpClient(); client.OnConnect = () => { Console.WriteLine("OnConnect"); string msg = "hello from sharpclient"; client.Write(Encoding.UTF8.GetBytes(msg)); }; client.OnClose = () => { Console.WriteLine("close now"); }; client.OnMessage = (byte[] msg) => { Console.WriteLine("read :" + Encoding.UTF8.GetString(msg)); }; client.Connect("localhost", 28800); } catch (Exception e) { Console.WriteLine(e); } Console.ReadKey(); }
static void Main(string[] args) { try { SharpClient client = new SharpClient(); client.SetChannel("test_1"); client.OnConnect = () => { Console.WriteLine("OnConnect"); string msg = "hello from sharpclient"; client.Write(Encoding.UTF8.GetBytes(msg)); }; client.OnClose = () => { Console.WriteLine("close now"); }; client.OnMessage = (byte[] msg) => { Console.WriteLine("read :" + Encoding.UTF8.GetString(msg)); }; //client.Connect("jgate.qipai.io", 18666);//公网 client.Connect("localhost", 18666);//内网 } catch (Exception e) { Console.WriteLine(e); } Console.ReadKey(); }
static void Main(string[] args) { // loop while running while (true) { Console.WriteLine("Attempting to connect to server"); client.Connect(host, port); // loop while connected while (client.Connected) { // recieve a message string message = client.ReceiveMessage(); // process a message (possibly on another thread?) ProcessMessage(message); } } }
// Use this for initialization void Start() { try { client.OnConnect = () => { UnityThread.Current.Queue(() => { Debug.Log("OnConnect"); string msg = "hello from sharpclient"; client.Write(Encoding.UTF8.GetBytes(msg)); } ); }; client.OnClose = () => { UnityThread.Current.Queue(() => { Debug.Log("close now"); }); }; client.OnMessage = (byte[] msg) => { UnityThread.Current.Queue(() => { Debug.Log("read :" + Encoding.UTF8.GetString(msg)); }); }; Debug.Log("connect now"); client.Connect("localhost", 28800); } catch (Exception e) { Debug.LogError(e); } }