WebSocket不能发送大数据



如何在Java Script中发送大数据到WebSocket ?

使用下面的代码,我能够发送126个字符的数据,但不超过这个。(c#编写)

public static void SendData(string text)
    {
        foreach (SocketClient client in ClientList)
        {
            if (client.Client.Connected)
            {
                try
                {
                    NetworkStream l_Stream = client.Client.GetStream();
                    List<byte> lb = new List<byte>();
                    lb = new List<byte>();
                    lb.Add(0x81);
                    int size = text.Length;
                    lb.Add((byte)size);
                    lb.AddRange(Encoding.UTF8.GetBytes(text));
                    l_Stream.Write(lb.ToArray(), 0, size + 2);
                }
                catch
                {
                    CloseClient(client);
                }
            }
        }
    }
}

有人能帮帮我吗?我试过很多方法,但没有一种对我有效。

我用的是Chrome 25。

这是一个websocket帧根据RFC 6455的样子:

  0                   1                   2                   3
  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 +-+-+-+-+-------+-+-------------+-------------------------------+
 |F|R|R|R| opcode|M| Payload len |    Extended payload length    |
 |I|S|S|S|  (4)  |A|     (7)     |             (16/64)           |
 |N|V|V|V|       |S|             |   (if payload len==126/127)   |
 | |1|2|3|       |K|             |                               |
 +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
 |     Extended payload length continued, if payload len == 127  |
 + - - - - - - - - - - - - - - - +-------------------------------+
 |                               |Masking-key, if MASK set to 1  |
 +-------------------------------+-------------------------------+
 | Masking-key (continued)       |          Payload Data         |
 +-------------------------------- - - - - - - - - - - - - - - - +
 :                     Payload Data continued ...                :
 + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
 |                     Payload Data continued ...                |
 +---------------------------------------------------------------+

设置websocket帧的有效载荷长度为字节1(第二个字节-第一个字节是字节0),一个字节只能有256个状态。但是字节1的第一个比特是用于屏蔽标志的。因此,您只能表示0到127之间的值。

当有效载荷长度大于125时,必须将字节1设置为126,并将长度放入字节2和字节3中("扩展有效载荷长度")。当您的有效载荷大于65535字节时,您必须将字节1设置为127,并将有效载荷长度设置为字节2-9("扩展有效载荷长度";和"扩展有效负载长度持续")。当您的有效载荷长度大于64位(16 Exabyte,或约1600万tb)时,那么……你应该派一堆装满硬盘的卡车来。

更多细节可以在官方Websocket RFC中找到

你可以发送任何大小的数据到WebSocket,但是大数据会被分割。

检查这个:https://datatracker.ietf.org/doc/html/draft-ietf-hybi-thewebsocketprotocol-17#section-5和5.4

pastebin.com/TxGZaHTY

http://buildnewgames.com/websockets/

引自本博客。在WebSocket服务器中发送和接收单个有效负载大小可能有限制。例如,在tomcat中,默认值是8192字节,在tomcat的doc中检查org.apache.tomcat.websocket.textBufferSize

因此,请确保WebSocket服务器中是否有有效载荷大小限制,如果是,则将其作为部分有效载荷接收。

package GoodExample;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.HashMap;
import sun.misc.BASE64Encoder;
/**
*
* @author
* Anders, Improved by Christopher Price
 */
public class WebRTCSignal {
public static final int MASK_SIZE = 4;
public static final int SINGLE_FRAME_UNMASKED = 0x81;
private ServerSocket serverSocket;
private Socket socket;
public WebRTCSignal() throws IOException {
serverSocket = new ServerSocket(1337);
connect();
}
private void connect() throws IOException {
System.out.println("Listening");
socket = serverSocket.accept();
System.out.println("Got connection");
if(handshake()) {
    listenerThread();
}
}
private boolean handshake() throws IOException {
PrintWriter out = new PrintWriter(socket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
HashMap<String, String> keys = new HashMap<>();
String str;
//Reading client handshake
while (!(str = in.readLine()).equals("")) {
    String[] s = str.split(": ");
    System.out.println();
    System.out.println(str);
    if (s.length == 2) {
    keys.put(s[0], s[1]);
    }
}
//Do what you want with the keys here, we will just use "Sec-WebSocket-Key"
String hash;
try {
    hash = new BASE64Encoder().encode(MessageDigest.getInstance("SHA-    1").digest((keys.get("Sec-WebSocket-Key") + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").getBytes()));
} catch (NoSuchAlgorithmException ex) {
    ex.printStackTrace();
    return false;
}
//Write handshake response
out.write("HTTP/1.1 101 Switching Protocolsrn"
    + "Upgrade: websocketrn"
    + "Connection: Upgradern"
    + "Sec-WebSocket-Accept: " + hash + "rn"
     + "Origin: http://face2fame.comrn"
    + "rn");
out.flush();
return true;
}
private byte[] readBytes(int numOfBytes) throws IOException {
byte[] b = new byte[numOfBytes];
socket.getInputStream().read(b);
return b;
}
public void sendMessage(byte[] msg) throws IOException {
System.out.println("Sending to client");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedOutputStream os = new BufferedOutputStream(socket.getOutputStream());
baos.write(SINGLE_FRAME_UNMASKED);
baos.write(msg.length);
baos.write(msg);
baos.flush();
baos.close();
convertAndPrint(baos.toByteArray());
os.write(baos.toByteArray(), 0, baos.size());
os.flush();
}
public void listenerThread() {
Thread t = new Thread(new Runnable() {
    @Override
    public void run() {
    try {
        while (true) {
        System.out.println("Recieved from client: " + reiceveMessage());
        System.out.println("Enter data to send");
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    }
});
t.start();
}
public String reiceveMessage() throws IOException {
byte[] buf = readBytes(2);
//dont use this byte[] buf2 = readBytes(4);
int extendedsize = 0;
System.out.println("Headers:");
if(!convertAndPrintHeader(buf)){ // This means we detected an extended message
    String CaryOverDetectiona = new String("");
    byte[] bufadder1 = buf.clone();
    byte[] bufadder2 = readBytes(2);
    byte[] array1and2 = new byte[bufadder1.length + bufadder2.length];
    System.arraycopy(bufadder1, 0, array1and2, 0, bufadder1.length);
    System.arraycopy(bufadder2, 0, array1and2, bufadder1.length, bufadder2.length);
    for (byte b : array1and2) {
        CaryOverDetectiona = (String.format("%02X ", b));
        System.out.print(CaryOverDetectiona);
}
    int i = ((array1and2[1] & 0xFF) - 0x80);
    //int c = ((array1and2[2] & 0xFF) - 0x80);
    //System.out.println(c+128);
    int j = ((array1and2[3] & 0xFF) - 0x80)+2;
    //System.out.println("The size of this uber message is" + j);
    extendedsize = i+j;
    System.out.println("Extended Size is" + extendedsize);
    //System.exit(0);
}
//convertAndPrint(buf2);// Check out the byte sizes
int opcode = buf[0] & 0x0F;
if (opcode == 8) {
    //Client want to close connection!
    System.out.println("Client closed!");
    socket.close();
    System.exit(0);
    return null;
} else {
   int payloadSize = getSizeOfPayload(buf[1]);
    if (extendedsize>=126){   
    payloadSize = extendedsize;}
    System.out.println("Payloadsize: " + payloadSize);
    buf = readBytes(MASK_SIZE + payloadSize);
    System.out.println("Payload:");
    convertAndPrint(buf);
    buf = unMask(Arrays.copyOfRange(buf, 0, 4), Arrays.copyOfRange(buf, 4, buf.length));
    String message = new String(buf);
    return message;
}
}
private int getSizeOfPayload(byte b) {
//Must subtract 0x80 from masked frames
int a = b & 0xff;
//System.out.println("PAYLOAD SIZE INT" + a);
return ((b & 0xFF) - 0x80);
}
private byte[] unMask(byte[] mask, byte[] data) {
for (int i = 0; i < data.length; i++) {
    data[i] = (byte) (data[i] ^ mask[i % mask.length]);
}
return data;
}
private boolean convertAndPrintHeader(byte[] bytes) {
   StringBuilder sb = new StringBuilder();
   String CaryOverDetection = new String();
   // We must test byte 2 specifically for this. In the next step we add length bytes perhaps?
   //for(int i = 0; i < bytes.length; i++) {
       //}
    for (byte b : bytes) {
        CaryOverDetection = (String.format("%02X ", b));
        if (CaryOverDetection.contains("FE")){
            return false;
        }
        sb.append(String.format("%02X ", b));
    }
    System.out.println(sb.toString());
    return true;
    }
private void convertAndPrint(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
    sb.append(String.format("%02X ", b));
}
System.out.println(sb.toString());
}
public static void main(String[] args) throws IOException, InterruptedException, NoSuchAlgorithmException {
WebRTCSignal j = new WebRTCSignal();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
   System.out.println("Write something to the client!");
   j.sendMessage(br.readLine().getBytes());
}
}
}

相关内容

  • 没有找到相关文章

最新更新