• Java-WebSocket 项目的研究(三) WebSocketClient 类 具体解释


    通过之前两篇文章

    Java-WebSocket 项目的研究(一) Java-WebSocket类图描写叙述


    Java-WebSocket 项目的研究(二) 小试身手:client连接server并发送消息实例

    的介绍我们大概了解到了整个项目的类结构,当中有一个重要的类:WebSocketClient,以下就让我们具体了解一下这个类


    首先看一下我们之前的类图关于WebSocketClient的描写叙述,能够看出:

    1.继承自WebSocketAdapter

    2.依赖于类WebSocketImpl(实际上关于WebSocket核心代码都在类WebSocketImpl里)

    3.实现了WebSocket接口(实际上通过类WebSocketImpl实现的)


    非常easy的,从字面意思我们就大概能猜到WebSocketAdapter是适配器类,架起了WebSocketImplWebSocketClient之间的桥梁,WebSocketImpl是web implementation的缩写,意思就是真正实现了websocket里基本的功能。


    然后我们看一下WebSocketClient的几个主要方法:

    首先是connect方法

    /**
    	 * Initiates the websocket connection. This method does not block.
    	 */
    	public void connect() {
    		if( writeThread != null )
    			throw new IllegalStateException( "WebSocketClient objects are not reuseable" );
    		writeThread = new Thread( this );
    		writeThread.start();
    	}
    

    我们能够发现:

    他事实上是起了一个线程,因为WebSocketClient类实现了Runnable接口,因此他会自己主动去调用run方法,然后我们进一步到run方法里去一探到底

    public void run() {
    		try {
    			if( socket == null ) {
    				socket = new Socket( proxy );
    			} else if( socket.isClosed() ) {
    				throw new IOException();
    			}
    			System.out.println("---->  "+uri.toString()+"   port: "+getPort() );
    			if( !socket.isBound() )
    				socket.connect( new InetSocketAddress( uri.getHost(), getPort() ), connectTimeout );
    			istream = socket.getInputStream();
    			ostream = socket.getOutputStream();
    
    			sendHandshake();
    		} catch ( /*IOException | SecurityException | UnresolvedAddressException | InvalidHandshakeException | ClosedByInterruptException | SocketTimeoutException */Exception e ) {
    			onWebsocketError( engine, e );
    			engine.closeConnection( CloseFrame.NEVER_CONNECTED, e.getMessage() );
    			return;
    		}
    
    		writeThread = new Thread( new WebsocketWriteThread() );
    		writeThread.start();
    
    		byte[] rawbuffer = new byte[ WebSocketImpl.RCVBUF ];
    		int readBytes;
    
    		try {
    			while ( !isClosed() && ( readBytes = istream.read( rawbuffer ) ) != -1 ) {
    				engine.decode( ByteBuffer.wrap( rawbuffer, 0, readBytes ) );
    			}
    			engine.eot();
    		} catch ( IOException e ) {
    			engine.eot();
    		} catch ( RuntimeException e ) {
    			// this catch case covers internal errors only and indicates a bug in this websocket implementation
    			onError( e );
    			engine.closeConnection( CloseFrame.ABNORMAL_CLOSE, e.getMessage() );
    		}
    		assert ( socket.isClosed() );
    	}
    


    以下我们对上面的代码进行具体研究:

    				socket = new Socket( proxy );
    

    这句显而易见,是创建了一个socket套接字。

    				socket.connect( new InetSocketAddress( uri.getHost(), getPort() ), connectTimeout );
    

    这句的意思是创建连接,參数就是server地址,port号,超时时间。

    			istream = socket.getInputStream();
    

    是接受server端的数据,关于接受的具体过程,我会在后期的博客中阐述,敬请期待。

    			ostream = socket.getOutputStream();
    

    是发送数据用的。

    其它的先不解释,我们继续之前的流程------connect之后就要发送信息,也就是调用send方法,send方法例如以下:

    /**
    	 * Sends <var>text</var> to the connected websocket server.
    	 * 
    	 * @param text
    	 *            The string which will be transmitted.
    	 */
    	public void send( String text ) throws NotYetConnectedException {
    		engine.send( text );
    	}

    它调用了engine的send方法,那么engine是啥东东呢,在类的声明处有这么一句话:

    	private WebSocketImpl engine = null;
    

    说明了WebSocketImpl类实现了send的操作。


    完。。。




  • 相关阅读:
    开源APM应用性能管理工具调研
    Inside ARC — to see the code inserted by the compiler
    报表应用系统中怎样正确使用图表功能
    创建cifs系统案例之“实现将Windows磁盘共享至Linux”
    Eclipse快捷键 10个最有用的快捷键
    如何生成KeyStore
    android中调用系统的发送短信、发送邮件、打电话功能
    android自带theme
    Android 报错:Conversion to Dalvik format failed: Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace.
    Android oncreate onupgrade什么时候被调用
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/3963745.html
Copyright © 2020-2023  润新知