1 //
2 // ========================================================================
3 // Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
17 //
18
19 package org.eclipse.jetty.websocket.api.io;
20
21 import java.io.IOException;
22 import java.nio.ByteBuffer;
23
24 import org.eclipse.jetty.websocket.api.RemoteEndpoint;
25 import org.eclipse.jetty.websocket.api.Session;
26
27 /**
28 * For working with the {@link Session} in a blocking technique.
29 * <p>
30 * This is an end-user accessible class.
31 */
32 public class WebSocketBlockingConnection
33 {
34 private final RemoteEndpoint remote;
35
36 public WebSocketBlockingConnection(Session session)
37 {
38 this.remote = session.getRemote();
39 }
40
41 /**
42 * Send a binary message.
43 * <p>
44 * Basic usage, results in a blocking write.
45 */
46 public void write(byte[] data, int offset, int length) throws IOException
47 {
48 ByteBuffer buf = ByteBuffer.wrap(data,offset,length);
49 remote.sendBytes(buf);
50 }
51
52 /**
53 * Send text message.
54 * <p>
55 * Basic usage, results in a blocking write.
56 */
57 public void write(String message) throws IOException
58 {
59 remote.sendString(message);
60 }
61 }