-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnityWebSocketExample.cs
More file actions
59 lines (47 loc) · 940 Bytes
/
Copy pathUnityWebSocketExample.cs
File metadata and controls
59 lines (47 loc) · 940 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
class ConnectAck
{
public ErrCode errCode;
}
public class UnityWebSocketExample : MonoBehaviour
{
public Text text;
WebSocketClient ws;
public void Connect()
{
//ws = new WebSocketClient(new Uri("ws://echo.websocket.org"));
ws = new WebSocketClient(new Uri("ws://127.0.0.1:20001"));
ws.Connect();
}
void OnSendCompleted(bool result)
{
return;
}
public void Send()
{
if (ws.isConnected == false)
{
return;
}
var req = new UserCreateReq();
{
req.userName = "userName";
req.passwd = "passwd";
}
var bytes = Encoding.UTF8.GetBytes(JsonUtility.ToJson(req));
ws.Send(bytes, OnSendCompleted);
}
public void Receive()
{
var bytes = ws.Receive();
if (bytes == null)
{
return;
}
var ack = JsonUtility.FromJson<ConnectAck>(Encoding.UTF8.GetString(bytes));
text.text = ack.errCode.ToString();
}
}