继续之前的教程,我们先下载项目源代码到本地,我们就能看到项目代码的结构如图一所示:
图一
然后,我们在eclipse中新建一个java project,并将java目录下的org目录整个拷进项目的src目录下(这是因为源代码中的包相对路径都是“org.java_websocket”,所以org必须在根目录下)。再将example目录拷进根目录下即可。这个example目录里有几个例子,对我们理解这个项目有很大的作用。
图二是笔者的项目截图:
图二
今天我们讲example中的ChatClient类和ChatServer类。很明显,一下就能开出来这两个类的作用,前者是聊天客户端,后者是聊天服务器。
我们先后右击文件ChatServer和ChatClient,选择Run As javaApplication即可运行(如图三)。
然后看效果(如图三):
如果出现跟笔者一样的效果,那么恭喜你,你的websocket已经连接服务器成功!
接下来,我们开始发送信息:
先定位到ChatClient的main方法:
- public static void main( String[] args ) {
- WebSocketImpl.DEBUG = true;
- String location;
- if( args.length != 0 ) {
- location = args[ 0 ];
- System.out.println( "Default server url specified: \'" + location + "\'" );
- } else {
- location = "ws://localhost:8887";
- System.out.println( "Default server url not specified: defaulting to \'" + location + "\'" );
- }
- new ChatClient( location );
- }
public static void main( String[] args ) {
WebSocketImpl.DEBUG = true;
String location;
if( args.length != 0 ) {
location = args[ 0 ];
System.out.println( "Default server url specified: \'" + location + "\'" );
} else {
location = "ws://localhost:8887";
System.out.println( "Default server url not specified: defaulting to \'" + location + "\'" );
}
new ChatClient( location );
}
我们分析一下这几句代码。
- WebSocketImpl.DEBUG = true;
WebSocketImpl.DEBUG = true;
这一句是开启debug模式,不多说了。
- location = "ws://localhost:8887";
location = "ws://localhost:8887";
这句是服务器地址,下面的
- new ChatClient( location );
new ChatClient( location );
就是调用了
ChatClient的构造函数。所以下面我们进到其构造函数中看一下,核心代码如下
- cc = new WebSocketClient( new URI( uriField.getText() ), (Draft) draft.getSelectedItem() ) {
- @Override
- public void onMessage( String message ) {
- ta.append( "got: " + message + "\n" );
- ta.setCaretPosition( ta.getDocument().getLength() );
- }
- @Override
- public void onOpen( ServerHandshake handshake ) {
- ta.append( "You are connected to ChatServer: " + getURI() + "\n" );
- ta.setCaretPosition( ta.getDocument().getLength() );
- }
- @Override
- public void onClose( int code, String reason, boolean remote ) {
- ta.append( "You have been disconnected from: " + getURI() + "; Code: " + code + " " + reason + "\n" );
- ta.setCaretPosition( ta.getDocument().getLength() );
- connect.setEnabled( true );
- uriField.setEditable( true );
- draft.setEditable( true );
- close.setEnabled( false );
- }
- @Override
- public void onError( Exception ex ) {
- ta.append( "Exception occured ...\n" + ex + "\n" );
- ta.setCaretPosition( ta.getDocument().getLength() );
- ex.printStackTrace();
- connect.setEnabled( true );
- uriField.setEditable( true );
- draft.setEditable( true );
- close.setEnabled( false );
- }
- };
cc = new WebSocketClient( new URI( uriField.getText() ), (Draft) draft.getSelectedItem() ) {
@Override
public void onMessage( String message ) {
ta.append( "got: " + message + "\n" );
ta.setCaretPosition( ta.getDocument().getLength() );
}
@Override
public void onOpen( ServerHandshake handshake ) {
ta.append( "You are connected to ChatServer: " + getURI() + "\n" );
ta.setCaretPosition( ta.getDocument().getLength() );
}
@Override
public void onClose( int code, String reason, boolean remote ) {
ta.append( "You have been disconnected from: " + getURI() + "; Code: " + code + " " + reason + "\n" );
ta.setCaretPosition( ta.getDocument().getLength() );
connect.setEnabled( true );
uriField.setEditable( true );
draft.setEditable( true );
close.setEnabled( false );
}
@Override
public void onError( Exception ex ) {
ta.append( "Exception occured ...\n" + ex + "\n" );
ta.setCaretPosition( ta.getDocument().getLength() );
ex.printStackTrace();
connect.setEnabled( true );
uriField.setEditable( true );
draft.setEditable( true );
close.setEnabled( false );
}
};
- //这句作用是连接服务器
- cc.connect();
- //这句是本人加的,用于发送消息
- cc.send("你好");
//这句作用是连接服务器
cc.connect();
//这句是本人加的,用于发送消息
cc.send("你好");
以上的代码作用是先初始化类WebSocketClient然后调用connect,最后发送消息。
完。。。