zoukankan      html  css  js  c++  java
  • WebSocket集成XMPP网页即时通讯3:二进制文件收发

    WebSocket支持二进制的发送,见jetty官网:

    http://www.eclipse.org/jetty/documentation/current/jetty-websocket-api-send-message.html

    Blocking Send Message 阻塞式

    Most calls are blocking in nature, and will not return until the send has completed (or has thrown an exception).

    RemoteEndpoint remote = session.getRemote();
     
    // Blocking Send of a BINARY message to remote endpoint
    ByteBuffer buf = ByteBuffer.wrap(new byte[] { 0x11, 0x22, 0x33, 0x44 });
    try
    {
        remote.sendBytes(buf);
    }
    catch (IOException e)
    {
        e.printStackTrace(System.err);
    }

    Send Partial Message 分块式

    RemoteEndpoint remote = session.getRemote();
     
    // Blocking Send of a BINARY message to remote endpoint
    // Part 1
    ByteBuffer buf1 = ByteBuffer.wrap(new byte[] { 0x11, 0x22 });
    // Part 2 (last part)
    ByteBuffer buf2 = ByteBuffer.wrap(new byte[] { 0x33, 0x44 });
    try
    {
        remote.sendPartialBytes(buf1,false);
        remote.sendPartialBytes(buf2,true); // isLast is true
    }
    catch (IOException e)
    {
        e.printStackTrace(System.err);
    }

     

    Async Send Message 异步

     //Example 29.7. Send Binary Message (Async Simple)
    
    RemoteEndpoint remote = session.getRemote();
     
    // Async Send of a BINARY message to remote endpoint
    ByteBuffer buf = ByteBuffer.wrap(new byte[] { 0x11, 0x22, 0x33, 0x44 });
    remote.sendBytesByFuture(buf);
    // Example 29.8. Send Binary Message (Async, Wait Till Success)
    
    RemoteEndpoint remote = session.getRemote();
     
    // Async Send of a BINARY message to remote endpoint
    ByteBuffer buf = ByteBuffer.wrap(new byte[] { 0x11, 0x22, 0x33, 0x44 });
    try
    {
        Future<Void> fut = remote.sendBytesByFuture(buf);
        // wait for completion (forever)
        fut.get();
    }
    catch (ExecutionException | InterruptedException e)
    {
        // Send failed
        e.printStackTrace();
    }
    
    How to send a simple Binary message using the RemoteEndpoint, tracking the Future<Void> to know if the send succeeded or failed.
    //Example 29.9. Send Binary Message (Async, timeout of send)
    
    
    RemoteEndpoint remote = session.getRemote();
     
    // Async Send of a BINARY message to remote endpoint
    ByteBuffer buf = ByteBuffer.wrap(new byte[] { 0x11, 0x22, 0x33, 0x44 });
    Future<Void> fut = null;
    try
    {
        fut = remote.sendBytesByFuture(buf);
        // wait for completion (timeout)
        fut.get(2,TimeUnit.SECONDS);
    }
    catch (ExecutionException | InterruptedException e)
    {
        // Send failed
        e.printStackTrace();
    }
    catch (TimeoutException e)
    {
        // timeout
        e.printStackTrace();
        if (fut != null)
        {
            // cancel the message
            fut.cancel(true);
        }
    }
     
  • 相关阅读:
    项目外部 property文件使用方法
    java JsonArray统一添加key
    java发送Http请求
    excel数据追加,java实现
    request.getParameter("name")获取参数为null和""空字符串的区别
    maven 配置jetty插件启动 及简单测试
    左右值编码实现树状存储
    Spring将classpath下的 .properties文件数据读出放到map中,在初始化时加载
    Ubuntu简单配置
    java.io.Serializable浅析
  • 原文地址:https://www.cnblogs.com/starcrm/p/5133221.html
Copyright © 2011-2022 走看看