前篇:
http://haoningabc.iteye.com/blog/2011985
代码的git地址
https://github.com/killinux/mysocket/tree/master/websocket/project/tomcat7servlet
如果是使用tomcat7的模式
1.html是客户端
2.tomcat7的servlet对websocket协议进行了封装,里面转发给普通的socket服务端
3.普通的socket 作为服务端
html ----->tomcat7,servlet----->socket server
这里纯js的客户端:
如果是用java-websocket,
git为 https://github.com/killinux/Java-WebSocket
代码如下:
socket server端:
----------------------------------------------------------------------
----------------------------------------------------------------------
如果server是用tomcat转发
参考
http://blog.csdn.net/a79412906/article/details/9393515
servlet:
web.xml配置:
tomcat7的
配置
中心server代码:
客户端改进如下:
servlet中发现,不能广播,只能单纯的返回,如果想每个浏览器都受到消息,则修改如下
这段参考
http://blog.fens.me/java-websocket-intro/
http://haoningabc.iteye.com/blog/2011985
代码的git地址
https://github.com/killinux/mysocket/tree/master/websocket/project/tomcat7servlet
如果是使用tomcat7的模式
1.html是客户端
2.tomcat7的servlet对websocket协议进行了封装,里面转发给普通的socket服务端
3.普通的socket 作为服务端
html ----->tomcat7,servlet----->socket server
这里纯js的客户端:
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>Insert title here</title>
- </head>
- <script type="text/javascript">
- var ws = null;
- function startServer() {
- var url = "ws://192.168.0.102:5000";
- if ('WebSocket' in window) {
- ws = new WebSocket(url);
- } else if ('MozWebSocket' in window) {
- ws = new MozWebSocket(url);
- } else {
- alert('浏览器不支持');
- return;
- }
- ws.onopen = function() {
- alert('Opened!');
- };
- // 收到服务器发送的文本消息, event.data表示文本内容
- ws.onmessage = function(event) {
- alert('Receive message: ' + event.data);
- };
- ws.onclose = function() {
- alert('Closed!');
- }
- }
- //发送信息
- function sendMessage(){
- var textMessage=document.getElementById("textMessage").value;
- if(ws!=null&&textMessage!=""){
- ws.send(textMessage);
- }
- }
- </script>
- <body οnlοad="startServer()">
- <input type="text" id="textMessage" size="20" />
- <input type="button" οnclick="sendMessage()" value="Send">
- </body>
- </html>
如果是用java-websocket,
git为 https://github.com/killinux/Java-WebSocket
代码如下:
socket server端:
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class Server {
- public static final int PORT = 5000;
- public static void main(String[] args) {
- System.out.println("服务器...\n");
- Server ser = new Server();
- ser.sock();
- }
- public void sock() {
- try {
- ServerSocket server = new ServerSocket(PORT);
- while (true) {
- // 一旦有堵塞, 则表示服务器与客户端获得了连接
- Socket client = server.accept();
- // 处理这次连接
- new PServer(client);
- }
- } catch (Exception e) {
- System.out.println("服务器异常: " + e.getMessage());
- }
- }
- private class PServer implements Runnable {
- private Socket socket;
- public PServer(Socket sock) {
- socket = sock;
- new Thread(this).start();
- }
- public void run() {
- System.out.println("一个客户端连接ip:" + socket.getInetAddress());
- try {
- // 读取客户端数据
- DataInputStream input = new DataInputStream(
- socket.getInputStream());
- // 向客户端发送数据
- DataOutputStream out = new DataOutputStream(
- socket.getOutputStream());
- // 读取客户端数据
- //System.out.println("客户端发过来的内容: " + input.readUTF());
- byte[] bt = new byte[1024];
- int leng = input.read(bt);
- System.out.println(new String(bt, 0, leng));
- // 发送键盘输入的一行
- // String s = new BufferedReader(new
- // InputStreamReader(System.in)).readLine();
- String s = "server d shu ru";
- out.write(s.getBytes());
- out.flush();
- input.close();
- out.close();
- socket.close();
- } catch (Exception e) {
- System.out.println("服务器 run 异常: " + e.getMessage());
- }
- }
- }
- }
----------------------------------------------------------------------
----------------------------------------------------------------------
如果server是用tomcat转发
参考
http://blog.csdn.net/a79412906/article/details/9393515
servlet:
- package com.hao;
- import java.io.DataInputStream;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import java.nio.ByteBuffer;
- import java.nio.CharBuffer;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.catalina.websocket.MessageInbound;
- import org.apache.catalina.websocket.StreamInbound;
- import org.apache.catalina.websocket.WebSocketServlet;
- public class HelloWorldWebSocketServlet extends WebSocketServlet {
- protected StreamInbound createWebSocketInbound(String subProtocol,HttpServletRequest arg1) {
- return new MessageInbound() {
- @Override
- protected void onBinaryMessage(ByteBuffer arg0) throws IOException {
- // TODO Auto-generated method stub
- }
- @Override
- protected void onTextMessage(CharBuffer message) throws IOException {
- // TODO Auto-generated method stub
- System.out.println("onText--->" + message.toString());
- Socket socket;
- String msg = "";
- try {
- // 向服务器利用Socket发送信息
- socket = new Socket("192.168.1.102", 5000);
- //socket = new Socket("127.0.0.1",5000);
- PrintWriter output = new PrintWriter(
- socket.getOutputStream());
- output.write(message.toString());
- output.flush();
- // 这里是接收到Server的信息
- DataInputStream input = new DataInputStream(
- socket.getInputStream());
- byte[] b = new byte[1024];
- input.read(b);
- // Server返回的信息
- msg = new String(b).trim();
- output.close();
- input.close();
- socket.close();
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- // 往浏览器发送信息
- CharBuffer cb = CharBuffer.wrap(new StringBuilder(msg));
- getWsOutbound().writeTextMessage(cb);
- }
- };
- }
- public static void main(String[] args) {
- Socket socket;
- String message ="haoning";
- String msg = "";
- try {
- // 向服务器利用Socket发送信息
- socket = new Socket("192.168.0.102", 5000);
- // socket = new Socket("127.0.0.1",5000);
- PrintWriter output = new PrintWriter(
- socket.getOutputStream());
- output.write(message.toString());
- output.flush();
- // 这里是接收到Server的信息
- DataInputStream input = new DataInputStream(
- socket.getInputStream());
- byte[] b = new byte[1024];
- input.read(b);
- // Server返回的信息
- msg = new String(b).trim();
- output.close();
- input.close();
- socket.close();
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
web.xml配置:
- <servlet>
- <servlet-name>wsSnake</servlet-name>
- <servlet-class>com.hao.HelloWorldWebSocketServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>wsSnake</servlet-name>
- <url-pattern>/websocket/test</url-pattern>
- </servlet-mapping>
tomcat7的
配置
- <?xml version='1.0' encoding='utf-8'?>
- <Context crossContext="true" docBase="D:/workspace/work1/Test/webapp" path="/webs" reloadable="true">
- </Context>
中心server代码:
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class Server {
- public static final int PORT = 5000;
- public static void main(String[] args) {
- System.out.println("服务器...\n");
- Server ser = new Server();
- ser.sock();
- }
- public void sock() {
- try {
- ServerSocket server = new ServerSocket(PORT);
- while (true) {
- // 一旦有堵塞, 则表示服务器与客户端获得了连接
- Socket client = server.accept();
- System.out.println("server accept");
- // 处理这次连接
- new PServer(client);
- }
- } catch (Exception e) {
- System.out.println("服务器异常: " + e.getMessage());
- }
- }
- private class PServer implements Runnable {
- private Socket socket;
- public PServer(Socket sock) {
- socket = sock;
- new Thread(this).start();
- }
- public void run() {
- System.out.println("一个客户端连接ip:" + socket.getInetAddress());
- try {
- // 读取客户端数据
- DataInputStream input = new DataInputStream(
- socket.getInputStream());
- // 向客户端发送数据
- DataOutputStream out = new DataOutputStream(
- socket.getOutputStream());
- // 读取客户端数据
- //System.out.println("客户端发过来的内容: " + input.readUTF());
- byte[] bt = new byte[1024];
- int leng = input.read(bt);
- System.out.println(new String(bt, 0, leng));
- // 发送键盘输入的一行
- // String s = new BufferedReader(new
- // InputStreamReader(System.in)).readLine();
- String s = "server d shu ru";
- out.write(s.getBytes());
- out.flush();
- input.close();
- out.close();
- socket.close();
- } catch (Exception e) {
- System.out.println("服务器 run 异常: " + e.getMessage());
- }
- }
- }
- }
客户端改进如下:
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>Insert title here</title>
- </head>
- <script type="text/javascript">
- var ws = null;
- function log(text) {
- document.getElementById("log").innerHTML = (new Date).getTime() + ": " + text +"<br>"+ document.getElementById("log").innerHTML;
- }
- function startServer() {
- var url = document.getElementById("serverip").value;// "ws://192.168.0.102:8887";
- if ('WebSocket' in window) {
- ws = new WebSocket(url);
- } else if ('MozWebSocket' in window) {
- ws = new MozWebSocket(url);
- } else {
- alert('浏览器不支持');
- return;
- }
- ws.onopen = function() {
- alert('Opened!');
- };
- // 收到服务器发送的文本消息, event.data表示文本内容
- ws.onmessage = function(event) {
- //alert('Receive message: ' + event.data);
- log('Receive message: ' + event.data);
- };
- ws.onclose = function() {
- alert('Closed!');
- }
- document.getElementById("conbtn").disabled="true";
- document.getElementById("stopbtn").removeAttribute('disabled');
- }
- //发送信息
- function sendMessage(){
- var textMessage=document.getElementById("textMessage").value;
- if(ws!=null&&textMessage!=""){
- ws.send(textMessage);
- }
- }
- function stopconn(){
- ws.close();
- document.getElementById("conbtn").removeAttribute('disabled');
- document.getElementById("stopbtn").disabled="true";
- }
- </script>
- <body οnlοad="">
- <input id="serverip" type="text" size="20" value="ws://192.168.0.102:8887"/>
- <input id="conbtn" type="button" οnclick="startServer()" value="open" />
- <input id="stopbtn" type="button" οnclick="stopconn()" value="stop" disabled="disabled"/>
- </br>
- <input id="textMessage" type="text" size="20" />
- <input type="button" οnclick="sendMessage()" value="Send">
- <div id="log"></div>
- </body>
- </html>
servlet中发现,不能广播,只能单纯的返回,如果想每个浏览器都受到消息,则修改如下
- package com.hao;
- import java.io.DataInputStream;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import java.nio.ByteBuffer;
- import java.nio.CharBuffer;
- import java.util.ArrayList;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.catalina.websocket.MessageInbound;
- import org.apache.catalina.websocket.StreamInbound;
- import org.apache.catalina.websocket.WebSocketServlet;
- import org.apache.catalina.websocket.WsOutbound;
- public class HelloWorldWebSocketServlet extends WebSocketServlet {
- private static ArrayList mmiList = new ArrayList();
- protected StreamInbound createWebSocketInbound(String subProtocol,
- HttpServletRequest arg1) {
- return new MyMessageInbound();
- }
- private class MyMessageInbound extends MessageInbound {
- WsOutbound myoutbound;
- @Override
- public void onOpen(WsOutbound outbound) {
- try {
- System.out.println("Open Client.");
- this.myoutbound = outbound;
- mmiList .add(this);
- outbound.writeTextMessage(CharBuffer.wrap("Hello!"));
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void onClose(int status) {
- System.out.println("Close Client.");
- mmiList .remove(this);
- }
- @Override
- protected void onBinaryMessage(ByteBuffer arg0) throws IOException {
- // TODO Auto-generated method stub
- }
- @Override
- protected void onTextMessage(CharBuffer message) throws IOException {
- // TODO Auto-generated method stub
- System.out.println("onText--->" + message.toString());
- for (int i=0;i< mmiList.size();i++ ) {
- MyMessageInbound mmib = (MyMessageInbound) mmiList.get(i);
- CharBuffer buffer = CharBuffer.wrap(message);
- mmib.myoutbound.writeTextMessage(buffer);
- mmib.myoutbound.flush();
- }
- /*Socket socket;
- String msg = "";
- try {
- // 向服务器利用Socket发送信息
- socket = new Socket("192.168.0.102", 5000);
- // socket = new Socket("127.0.0.1",5000);
- PrintWriter output = new PrintWriter(socket.getOutputStream());
- output.write(message.toString());
- output.flush();
- // 这里是接收到Server的信息
- DataInputStream input = new DataInputStream(
- socket.getInputStream());
- byte[] b = new byte[1024];
- input.read(b);
- // Server返回的信息
- msg = new String(b).trim();
- output.close();
- input.close();
- socket.close();
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- // 往浏览器发送信息
- CharBuffer cb = CharBuffer.wrap(new StringBuilder(msg));
- getWsOutbound().writeTextMessage(cb);*/
- }
- }
- public static void main(String[] args) {
- Socket socket;
- String message = "haoning";
- String msg = "";
- try {
- // 向服务器利用Socket发送信息
- socket = new Socket("192.168.0.102", 5000);
- // socket = new Socket("127.0.0.1",5000);
- PrintWriter output = new PrintWriter(socket.getOutputStream());
- output.write(message.toString());
- output.flush();
- // 这里是接收到Server的信息
- DataInputStream input = new DataInputStream(socket.getInputStream());
- byte[] b = new byte[1024];
- input.read(b);
- // Server返回的信息
- msg = new String(b).trim();
- output.close();
- input.close();
- socket.close();
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
这段参考
http://blog.fens.me/java-websocket-intro/
ChatClient.java
ChatServer.java
EmptyClient.java
WebSocket.jar
index.html
EmptyClient.java
ChatServer.java
ChatServer.java
EmptyClient.java
WebSocket.jar
index.html
- index.html
- <!DOCTYPE html>
- <html>
- <head>
- <title>WebSocket Chat Client</title>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <script type="text/javascript" src="prototype.js"></script>
- <script type="text/javascript">
- document.observe("dom:loaded", function() {
- function log(text) {
- $("log").innerHTML = (new Date).getTime() + ": " + (!Object.isUndefined(text) && text !== null ? text : "null") +"<br>"+ $("log").innerHTML;
- }
- if (!window.WebSocket) {
- alert("FATAL: WebSocket not natively supported. This demo will not work!");
- }
- var ws;
- $("uriForm").observe("submit", function(e) {
- e.stop();
- ws = new WebSocket($F("uri"));
- ws.onopen = function() {
- log("[WebSocket#onopen]");
- }
- ws.onmessage = function(e) {
- log("<b style='color:red;'>[WebSocket#onmessage] 接收消息:" + e.data + "</b>");
- }
- ws.onclose = function() {
- log("[WebSocket#onclose]");
- $("uri", "connect").invoke("enable");
- $("disconnect").disable();
- ws = null;
- }
- $("uri", "connect").invoke("disable");
- $("disconnect").enable();
- });
- $("sendForm").observe("submit", function(e) {
- e.stop();
- if (ws) {
- var textField = $("textField");
- ws.send(textField.value);
- //log("[WebSocket#send] 发送消息:" + textField.value );
- textField.value = "";
- textField.focus();
- }
- });
- $("clear").observe("click", function(e) {
- $("log").innerHTML ='';
- });
- $("disconnect").observe("click", function(e) {
- e.stop();
- if (ws) {
- ws.close();
- ws = null;
- }
- });
- });
- </script>
- </head>
- <body>
- <form id="uriForm"><input type="text" id="uri" value="ws://localhost:8887" style="width:200px;"> <input type="submit" id="connect" value="Connect"><input type="button" id="disconnect" value="Disconnect" disabled="disabled"></form><br>
- <form id="sendForm"><input type="text" id="textField" value="" style="width:200px;"> <input type="submit" value="Send"> <input type="button" id='clear' value="clear msg"></form><br>
- <form><div id="log" style="width:800px;heigth:450px" ></div></form><br>
- </body>
- </html>
EmptyClient.java
- import java.net.URI;
- import org.java_websocket.WebSocketClient;
- import org.java_websocket.drafts.Draft;
- import org.java_websocket.handshake.ServerHandshake;
- public class EmptyClient extends WebSocketClient {
- public EmptyClient( URI serverUri , Draft draft ) {
- super( serverUri, draft );
- }
- public EmptyClient( URI serverURI ) {
- super( serverURI );
- }
- @Override
- public void onOpen( ServerHandshake handshakedata ) {
- }
- @Override
- public void onMessage( String message ) {
- }
- @Override
- public void onClose( int code, String reason, boolean remote ) {
- }
- @Override
- public void onError( Exception ex ) {
- }
- }
ChatServer.java
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.InetAddress;
- import java.net.InetSocketAddress;
- import java.net.UnknownHostException;
- import java.util.Set;
- import org.java_websocket.WebSocket;
- import org.java_websocket.WebSocketServer;
- import org.java_websocket.handshake.ClientHandshake;
- public class ChatServer extends WebSocketServer {
- public ChatServer( int port ) throws UnknownHostException {
- super( new InetSocketAddress( InetAddress.getByName( "localhost" ), port ) );
- }
- public ChatServer( InetSocketAddress address ) {
- super( address );
- }
- @Override
- public void onOpen( WebSocket conn, ClientHandshake handshake ) {
- try {
- this.sendToAll( "new" );
- } catch ( InterruptedException ex ) {
- ex.printStackTrace();
- }
- System.out.println( conn + " entered the room!" );
- }
- @Override
- public void onClose( WebSocket conn, int code, String reason, boolean remote ) {
- try {
- this.sendToAll( conn + " has left the room!" );
- } catch ( InterruptedException ex ) {
- ex.printStackTrace();
- }
- System.out.println( conn + " has left the room!" );
- }
- @Override
- public void onMessage( WebSocket conn, String message ) {
- try {
- this.sendToAll( message );
- } catch ( InterruptedException ex ) {
- ex.printStackTrace();
- }
- System.out.println( conn + ": " + message );
- }
- @Override
- public void onError( WebSocket conn, Exception ex ) {
- ex.printStackTrace();
- }
- public void sendToAll( String text ) throws InterruptedException {
- Set<WebSocket> con = connections();
- synchronized ( con ) {
- for( WebSocket c : con ) {
- c.send( text );
- }
- }
- }
- ///
- public static void main( String[] args ) throws InterruptedException , IOException {
- //连接部份
- WebSocket.DEBUG = true;
- int port = 8887;
- try {
- port = Integer.parseInt( args[ 0 ] );
- } catch ( Exception ex ) { }
- ChatServer s = new ChatServer( port );
- s.start();
- System.out.println( "ChatServer started on port: " + s.getPort() );
- //服务端 发送消息处理部份
- BufferedReader sysin = new BufferedReader( new InputStreamReader( System.in ) );
- while ( true ) {
- String in = sysin.readLine();
- s.sendToAll( in );
- }
- }
- }
- import java.awt.Container;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowEvent;
- import java.net.URI;
- import java.net.URISyntaxException;
- import javax.swing.JButton;
- import javax.swing.JComboBox;
- import javax.swing.JFrame;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- import org.java_websocket.WebSocket;
- import org.java_websocket.WebSocketClient;
- import org.java_websocket.drafts.Draft;
- import org.java_websocket.drafts.Draft_10;
- import org.java_websocket.drafts.Draft_17;
- import org.java_websocket.drafts.Draft_75;
- import org.java_websocket.drafts.Draft_76;
- import org.java_websocket.handshake.ServerHandshake;
- public class ChatClient extends JFrame implements ActionListener {
- private static final long serialVersionUID = -6056260699202978657L;
- private final JTextField uriField;
- private final JButton connect;
- private final JButton close;
- private final JTextArea ta;
- private final JTextField chatField;
- private final JComboBox draft;
- private WebSocketClient cc;
- public ChatClient( String defaultlocation ) {
- super( "WebSocket Chat Client" );
- Container c = getContentPane();
- GridLayout layout = new GridLayout();
- layout.setColumns( 1 );
- layout.setRows( 6 );
- c.setLayout( layout );
- Draft[] drafts = { new Draft_17(), new Draft_10(), new Draft_76(), new Draft_75() };
- draft = new JComboBox( drafts );
- c.add( draft );
- uriField = new JTextField();
- uriField.setText( defaultlocation );
- c.add( uriField );
- connect = new JButton( "Connect" );
- connect.addActionListener( this );
- c.add( connect );
- close = new JButton( "Close" );
- close.addActionListener( this );
- close.setEnabled( false );
- c.add( close );
- JScrollPane scroll = new JScrollPane();
- ta = new JTextArea();
- scroll.setViewportView( ta );
- c.add( scroll );
- chatField = new JTextField();
- chatField.setText( "" );
- chatField.addActionListener( this );
- c.add( chatField );
- java.awt.Dimension d = new java.awt.Dimension( 300, 400 );
- setPreferredSize( d );
- setSize( d );
- addWindowListener( new java.awt.event.WindowAdapter() {
- @Override
- public void windowClosing( WindowEvent e ) {
- if( cc != null ) {
- cc.close();
- }
- dispose();
- }
- } );
- setLocationRelativeTo( null );
- setVisible( true );
- }
- public void actionPerformed( ActionEvent e ) {
- if( e.getSource() == chatField ) {
- if( cc != null ) {
- try {
- cc.send( chatField.getText() );
- chatField.setText( "" );
- chatField.requestFocus();
- } catch ( InterruptedException ex ) {
- ex.printStackTrace();
- }
- }
- } else if( e.getSource() == connect ) {
- try {
- // cc = new ChatClient(new URI(uriField.getText()), area, ( Draft ) draft.getSelectedItem() );
- 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 );
- }
- };
- close.setEnabled( true );
- connect.setEnabled( false );
- uriField.setEditable( false );
- draft.setEditable( false );
- cc.connect();
- } catch ( URISyntaxException ex ) {
- ta.append( uriField.getText() + " is not a valid WebSocket URI\n" );
- }
- } else if( e.getSource() == close ) {
- try {
- cc.close();
- } catch ( Exception ex ) {
- ex.printStackTrace();
- }
- }
- }
- public static void main( String[] args ) {
- WebSocket.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 );
- }
- }