001/*
002 *  $Id: TomsBinaryEventSocket.java 997 2015-08-21 00:06:40Z tgutwin $
003 *  $HeadURL:  $
004 *  $Revision: 997 $
005 *  $LastChangedDate: 2015-08-20 17:06:40 -0700 (Thu, 20 Aug 2015) $
006 *  $LastChangedBy: tgutwin $
007 *  Copyright (c) 2017 Tom B. Gutwin P.Eng. North Vancouver BC Canada
008 *
009 *  This program is free software; you can redistribute it and/or
010 *  modify it under the terms of the GNU General Public License
011 *  as published by the Free Software Foundation; either version 3
012 *  of the License, or any later version.
013 *
014 *  This program is distributed in the hope that it will be useful,
015 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
016 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 *  GNU General Public License for more details.
018 *
019 *  You should have received a copy of the GNU General Public License
020 *  along with this program; If not, see <http://www.gnu.org/licenses/>.
021 */
022
023package ca.bc.webarts.servlet;
024
025
026import java.util.concurrent.CountDownLatch;
027import java.util.concurrent.Future;
028import java.util.concurrent.TimeUnit;
029
030import org.eclipse.jetty.websocket.api.Session;
031import org.eclipse.jetty.websocket.api.WebSocketAdapter;
032
033/** A very basic event socket impl that the TomsWebsocketEchoServlet uses to handle webSocket Text Events. **/
034public class TomsBinaryEventSocket extends WebSocketAdapter
035{
036  Session session_ = null;
037  private final CountDownLatch closeLatch = null;
038
039
040  @SuppressWarnings("unused")
041  public TomsBinaryEventSocket()
042  {
043    super();
044  }
045
046
047  @Override
048  public void onWebSocketConnect(Session sess)
049  {
050      super.onWebSocketConnect(sess);
051      System.out.println("Socket Connected: " + sess);
052      session_ = sess;
053  }
054
055
056  @Override
057  public void onWebSocketBinary(byte[] payload, int offset, int len)
058  {
059    super.onWebSocketBinary(payload,  offset,  len);
060    String img = "";
061    System.out.println("Received BINARY message: " );
062    try
063    {
064      if(session_!=null)
065      {
066        session_.getRemote().sendString("  Received["+len+"]: "+payload);
067        if(payload!=null && len>0 )
068        {
069          System.out.println("  )))) Socket Received binary payload: "  );
070          try
071          {
072
073          }
074          catch (Exception e)
075          {
076              // TODO Auto-generated catch block
077              e.printStackTrace();
078          }
079        }
080      }
081    }
082    catch (java.io.IOException ioEx)
083    {
084      System.out.println("ERROR: can't get remote host connection");
085    }
086  }
087
088
089  @Override
090  public void onWebSocketClose(int statusCode, String reason)
091  {
092      super.onWebSocketClose(statusCode,reason);
093      System.out.println("Socket Closed: [" + statusCode + "] " + reason);
094      session_ = null;
095  }
096
097
098  @Override
099  public void onWebSocketError(Throwable cause)
100  {
101      super.onWebSocketError(cause);
102      cause.printStackTrace(System.err);
103  }
104}