大神这个JS代码转C#代码怎么转

JirlangLianHongCh 2019-05-27 11:19:11
 abc1: function (e) {
for (var n = new ArrayBuffer(2 * e.length), r = new Uint16Array(n), t = 0, o = e
.length; t < o; t++) r[t] = e.charCodeAt(t);
return r;
},
json2buffer: function (n) {
var r = this.abc1(JSON.stringify(n)),
t = r.length,
o = new ArrayBuffer(4);
new DataView(o).setUint32(0, t);
var s = new Uint8Array(4 + t);
return s.set(new Uint8Array(o), 0), s.set(r, 4), s.buffer;
},

sendMessage: function (message) {
this.socket.send(this.json2buffer(message));
},

getInfo: function () {
var e = {
request_type: "1001",
longtitude: "longitude",
latitude: "latitude",
requestid:"1001",
platform: 0
};
this.sendMessage(e, "1001");
}
...全文
163 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
stherix 2019-05-30
  • 打赏
  • 举报
回复
async Task OnMessage(MessageEventArgs e) { //读取e.Data进行操作 //可选 await Task.Yield(); }
JirlangLianHongCh 2019-05-30
  • 打赏
  • 举报
回复
引用 9 楼 stherix 的回复:
[quote=引用 8 楼 JirlangLianHongCh 的回复:] [quote=引用 6 楼 stherix 的回复:] 那还是用第三方的库吧 .net内置的websocket还真的只能在win8+使用
其他库好像 send 都不支持 byte[] 有没有封装JS的办法 实现WSS 链接?[/quote] 都支持的啊 websocket-sharp就支持byte[] ,string, stream,其他库应该也差不多[/quote]试了这个DotNetProjects.websocket-sharp 怎么取回服务器返回的数据?webSocket.OnMessage 这个怎么用
JirlangLianHongCh 2019-05-30
  • 打赏
  • 举报
回复
引用 14 楼 stherix 的回复:
另外 websocket连接建立后不要立刻关掉 这应该是长连接,否则无法收到服务端发来的数据
应该是OK了
stherix 2019-05-30
  • 打赏
  • 举报
回复
另外 websocket连接建立后不要立刻关掉 这应该是长连接,否则无法收到服务端发来的数据
stherix 2019-05-30
  • 打赏
  • 举报
回复
哦,应该是js的大小端转换和c#有区别 最前面长度4字节,顺序要翻过来才行 var length = BitConverter.GetBytes(data.Length).Reverse().ToArray();
JirlangLianHongCh 2019-05-30
  • 打赏
  • 举报
回复
引用 11 楼 stherix 的回复:
async Task OnMessage(MessageEventArgs e) { //读取e.Data进行操作 //可选 await Task.Yield(); }
非常感谢指点. 但算法转换好像有点问题 服务器不接受报错了 The header of a frame cannot be read from the stream.[face]qq:54.gif[/face ]
async static void ttt(byte[] ylzy)
        {
            WebSocket webSocket = new WebSocket("wss://publicld.gwgo.qq.com?account_value=0&account_type=0&appid=0&token=0");
            try
            {
               
                webSocket.OnMessage += (sender, e) =>
                Console.WriteLine("Laputa says: " + e.Data);
                
                webSocket.Connect();

                webSocket.Send(ylzy);

            }
            catch(Exception ex)
            {
                webSocket.Close();
                Console.WriteLine("Laputa says: " + ex.Message);
            }


        }
private void Button1_Click(object sender, EventArgs e)
        {
            Person p = new Person();
            p.request_type = "1001";
            p.latitude = 39962698;
            p.requestid = 907631;//GetTime() % 1234567;
            p.platform = 0;
            p.longtitude = 116357553;
            
            JavaScriptSerializer jss = new JavaScriptSerializer();
            string JsonStr = jss.Serialize(p);

            //var obj = new{request_type = "1001",longtitude = 121474328,latitude = 31234637,requestid = GetTime() % 1234567, platform = 0};
            //var json = JsonConvert.SerializeObject(obj, Formatting.Indented);
            var data = Encoding.ASCII.GetBytes(JsonStr);
            var length = BitConverter.GetBytes(data.Length);
            var packet = new byte[4 + data.Length];
            length.CopyTo(packet, 0);
            data.CopyTo(packet, 4);

            ttt(packet);
            //这里是websocket发送,自己创建连接吧
            //socket.SendAsync(packet, WebSocketMessageType.Binary, true, cancelToken);
        }
stherix 2019-05-29
  • 打赏
  • 举报
回复
引用 8 楼 JirlangLianHongCh 的回复:
[quote=引用 6 楼 stherix 的回复:] 那还是用第三方的库吧 .net内置的websocket还真的只能在win8+使用
其他库好像 send 都不支持 byte[] 有没有封装JS的办法 实现WSS 链接?[/quote] 都支持的啊 websocket-sharp就支持byte[] ,string, stream,其他库应该也差不多
JirlangLianHongCh 2019-05-29
  • 打赏
  • 举报
回复
引用 6 楼 stherix 的回复:
那还是用第三方的库吧 .net内置的websocket还真的只能在win8+使用
其他库好像 send 都不支持 byte[] 有没有封装JS的办法 实现WSS 链接?
stherix 2019-05-29
  • 打赏
  • 举报
回复
另外 so上面说在nuget上获取System.Net.WebSockets.Client.Managed可以在win7用websocket客户端 System.Net.WebSockets.Client.Managed.ClientWebSocket
stherix 2019-05-29
  • 打赏
  • 举报
回复
那还是用第三方的库吧 .net内置的websocket还真的只能在win8+使用
JirlangLianHongCh 2019-05-29
  • 打赏
  • 举报
回复
System.Reflection.TargetInvocationException HResult=0x80131604 Message=调用的目标发生了异常。 Source=mscorlib StackTrace: 在 System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) 在 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) 在 System.Delegate.DynamicInvokeImpl(Object[] args) 在 System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme) 在 System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj) 在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 在 System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme) 在 System.Windows.Forms.Control.InvokeMarshaledCallbacks() 在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 在 System.Windows.Forms.Application.Run(Form mainForm) 在 WindowsFormsApp1.Program.Main() 在 F:\jg\WindowsFormsApp1\WindowsFormsApp1\Program.cs 中: 第 19 行 内部异常 1: PlatformNotSupportedException: 此平台不支持 WebSocket 协议。
引用 4 楼 stherix 的回复:
[quote=引用 3 楼 JirlangLianHongCh 的回复:] [quote=引用 1 楼 stherix 的回复:]
var obj = new
{
    request_type = "1001",
    longtitude = "longitude",
    latitude = "latitude",
    requestid = "1001",
    platform = 0
};
var json = JsonConvert.SerializeObject(obj, Formatting.Indented);
var data = Encoding.ASCII.GetBytes(json);
var length = BitConverter.GetBytes(data.Length);
var packet = new byte[4 + data.Length];
length.CopyTo(packet, 0);
data.CopyTo(packet, 4);
//这里是websocket发送,自己创建连接吧
socket.SendAsync(packet, WebSocketMessageType.Binary, true, cancelToken);
谢谢大神 win7是不是不支持 websocket?[/quote] .net4.5以上内置支持,win7也一样 如果不到4.5,可以找第三方库[/quote]
stherix 2019-05-29
  • 打赏
  • 举报
回复
引用 3 楼 JirlangLianHongCh 的回复:
[quote=引用 1 楼 stherix 的回复:]
var obj = new
{
    request_type = "1001",
    longtitude = "longitude",
    latitude = "latitude",
    requestid = "1001",
    platform = 0
};
var json = JsonConvert.SerializeObject(obj, Formatting.Indented);
var data = Encoding.ASCII.GetBytes(json);
var length = BitConverter.GetBytes(data.Length);
var packet = new byte[4 + data.Length];
length.CopyTo(packet, 0);
data.CopyTo(packet, 4);
//这里是websocket发送,自己创建连接吧
socket.SendAsync(packet, WebSocketMessageType.Binary, true, cancelToken);
谢谢大神 win7是不是不支持 websocket?[/quote] .net4.5以上内置支持,win7也一样 如果不到4.5,可以找第三方库
JirlangLianHongCh 2019-05-29
  • 打赏
  • 举报
回复
引用 1 楼 stherix 的回复:
var obj = new
{
    request_type = "1001",
    longtitude = "longitude",
    latitude = "latitude",
    requestid = "1001",
    platform = 0
};
var json = JsonConvert.SerializeObject(obj, Formatting.Indented);
var data = Encoding.ASCII.GetBytes(json);
var length = BitConverter.GetBytes(data.Length);
var packet = new byte[4 + data.Length];
length.CopyTo(packet, 0);
data.CopyTo(packet, 4);
//这里是websocket发送,自己创建连接吧
socket.SendAsync(packet, WebSocketMessageType.Binary, true, cancelToken);
谢谢大神 win7是不是不支持 websocket?
weixin_44501825 2019-05-29
  • 打赏
  • 举报
回复
stherix 2019-05-28
  • 打赏
  • 举报
回复
var obj = new
{
    request_type = "1001",
    longtitude = "longitude",
    latitude = "latitude",
    requestid = "1001",
    platform = 0
};
var json = JsonConvert.SerializeObject(obj, Formatting.Indented);
var data = Encoding.ASCII.GetBytes(json);
var length = BitConverter.GetBytes(data.Length);
var packet = new byte[4 + data.Length];
length.CopyTo(packet, 0);
data.CopyTo(packet, 4);
//这里是websocket发送,自己创建连接吧
socket.SendAsync(packet, WebSocketMessageType.Binary, true, cancelToken);

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧