• 请不要在回答技术问题时复制粘贴 AI 生成的内容
xoxo419
V2EX  ›  程序员

微信小程序如何实现 byte 发送?

  •  
  •   xoxo419 · Oct 18, 2017 · 4276 views
    This topic created in 3153 days ago, the information mentioned may be changed or developed.

    需要利用小程序 websocket 发送 byte.

    php 代码

    <?php
    $str0 = '01 00 00 16 00 00 00 9c 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 /r /n';
    
    $byte_str0 = tobyte($str0); 
    
    function tobyte($str)
    {
    	$arr = str_split(str_ireplace(' ', '', $str), 2);//去掉空格后,2 字符一段切分成数组 
    	$byte_arr = array();
    	foreach ($arr as $h) { 
    		$byte_arr[] = chr(hexdec($h));
    	}
    
    	$byte_str = implode('', $byte_arr); 
    	return $byte_str;
    }
    
    
    1. 上面这段如何用 JS 实现?
    2. ArrayBuffe 与上面的有关系?
    3 replies    2017-10-19 09:32:45 +08:00
    xoxo419
        1
    xoxo419  
    OP
       Oct 18, 2017
    java 使用的 socket tcp 协议, 接收的数据类型为 byte, 微信小程序的 websocket 需要的 ws 协议, 于是使用 node.js 做了中转. 那么 javascript 如何发送 byte 的? ArrayBuffer 可以直接发?
    meanmachine
        2
    meanmachine  
       Oct 19, 2017
    这个 \r\n 看着有点别扭
    nutting
        3
    nutting  
       Oct 19, 2017
    websocket,感觉属于 http 层的东西了吧,做不到 byte 级别,只能想法转换了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2847 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 02:48 · PVG 10:48 · LAX 19:48 · JFK 22:48
    ♥ Do have faith in what you're doing.