// Use this for initialization void Start() { // testT(); string url = "http://www.caishenlaile.com/game/basic/login"; HttpProxy.SendPostRequest <LoginMessage>(url, new Dictionary <string, object>(), (res, msg) => { // Debug.Log(user.data.ToString()); if (!msg.isOK) { Debug.Log(msg.errmsg); } else { Debug.Log(msg.ToString()); wsp = new WebSocketProxy("ws://10.0.2.55:8080/ws/conn?uid=2"); wsp.onOpen += () => { Debug.Log("on open"); wsp.send("{\"cmd\":\"info\",\"fromid\":\"1\",\"toid\":\"2\",\"content\":\"hello?\"}"); }; wsp.onMessage += (s) => { Debug.Log("onMessage" + s); }; wsp.onError += (s) => { Debug.Log("onError"); }; } }); }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); var webSocketOptions = new WebSocketOptions { KeepAliveInterval = TimeSpan.FromSeconds(120), ReceiveBufferSize = 1 * 1024, }; app.UseWebSockets(webSocketOptions); app.UseEndpoints(endpoints => { endpoints.Map("/game/{ip}/{port}", async context => { var ip = (string)context.GetRouteValue("ip"); var port = (string)context.GetRouteValue("port"); if (!IPAddress.TryParse(ip, out var ipAddress) || ipAddress.AddressFamily != AddressFamily.InterNetwork || ip.Count(c => c == '.') != 3) { context.Response.StatusCode = 400; return; } if (!ushort.TryParse(port, out var parsedPort) || parsedPort < 10000) { context.Response.StatusCode = 400; return; } if (!context.WebSockets.IsWebSocketRequest) { context.Response.StatusCode = 400; return; } var socket = await context.WebSockets.AcceptWebSocketAsync(); await WebSocketProxy.Run(context, socket, new IPEndPoint(ipAddress, parsedPort)); }); endpoints.MapGet("/", async context => { await context.Response.WriteAsync("Hello World!"); }); }); }