Commit 3ecef069 by TongZuu

init

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path=""/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="lib" path="/Users/csc-mac/Documents/EnterprisePT/jar/websocket/gson.jar"/>
<classpathentry kind="lib" path="/Users/csc-mac/Documents/EnterprisePT/jar/websocket/jboss-websocket-api_1.1_spec-1.1.1.Final.jar"/>
<classpathentry kind="lib" path="/Users/csc-mac/Documents/EnterprisePT/jar/websocket/tyrus-standalone-client-1.12.jar"/>
<classpathentry kind="output" path=""/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>TestWebsocket2</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
/CreateClient.class
/RunableWebSocket.class
package example.controller;
import example.websocket.WebsocketEndpoint;
import example.websocket.config.WebsocketConfig;
import example.websocket.factory.WebSocketFactory;
public class CreateClient implements Runnable{
private String name = Thread.currentThread().getName();
private int size = 0;
public CreateClient(int size) {
this.size = size;
}
public CreateClient(String name , int size){
this.name = name;
this.size = size;
}
@Override
public void run() {
for(int i = 0; i < this.size; i++){
try {
WebsocketEndpoint ws = WebSocketFactory.getInstance().createEndPoint(WebsocketConfig.classEndPoint);
new Thread(ws , this.name +"_"+ i).start();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
package example.controller;
import example.websocket.WebsocketEndpoint;
import example.websocket.config.WebsocketConfig;
import example.websocket.factory.WebSocketFactory;
public class RunableWebSocket {
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
if(WebsocketConfig.maximumClient > 1000){
int averageSize = WebsocketConfig.maximumClient / 10;
for(int i = 0; i < 10; i++){
new Thread(new CreateClient(averageSize)).start();
}
}else{
new Thread(new CreateClient(WebsocketConfig.maximumClient)).start();
}
}
}
/TestWebsocket.class
package example.test;
import java.io.IOException;
import java.net.URISyntaxException;
import javax.websocket.DeploymentException;
import javax.websocket.Session;
import example.websocket.EndpointBase;
public class TestWebsocket extends EndpointBase{
public TestWebsocket() throws DeploymentException, IOException, URISyntaxException {
super();
}
@Override
public void onMessage(Session session, String message) {
String x = "";
}
@Override
public void alive() {
this.sendMessage("xxxx");
}
}
/EndpointBase$1.class
/EndpointBase.class
/WebsocketEndpoint.class
package example.websocket;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import javax.websocket.DeploymentException;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.SendHandler;
import javax.websocket.SendResult;
import javax.websocket.Session;
import example.websocket.factory.ClientEndpointFactory;
public abstract class EndpointBase extends Endpoint implements WebsocketEndpoint{
private Session session = null;
private boolean runing = true;
protected long minimum = 5000;
protected long maximum = 15000;
public EndpointBase() throws DeploymentException, IOException, URISyntaxException {
ClientEndpointFactory.getInstance().connect(this);
}
@Override
final public void run() {
if(session == null){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
while(runing){
alive();
try {
Thread.sleep(minimum + (int)(Math.random() * maximum));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public abstract void alive();
public abstract void onMessage(Session session , String message);
@Override
final public void onOpen(Session arg0, EndpointConfig arg1) {
session = arg0;
notify();
}
final public void sendMessage(String message){
//session.getAsyncRemote().sendText(message);
session.getAsyncRemote().sendText(message, new SendHandler() {
@Override
public void onResult(SendResult arg0) {
String x = "";
}
});
}
final public void sendMessage(ByteBuffer buffer){
session.getAsyncRemote().sendBinary(buffer);
}
final public void sendMessage(Object obj){
session.getAsyncRemote().sendObject(obj);
}
final public void shutdown(){
this.runing = false;
}
}
package example.websocket;
import javax.websocket.Session;
public interface WebsocketEndpoint extends Runnable{
public void shutdown();
}
package example.websocket.config;
import example.test.TestWebsocket;
import example.websocket.EndpointBase;
import example.websocket.WebsocketEndpoint;
public class WebsocketConfig {
//public static String URIPATH = "ws://localhost:8080/hr/CSCWS";
public static String URIPATH = "ws://10.01.1.91:8080/hr/TESTWS2";
public static Class classEndPoint = TestWebsocket.class;
public static int maximumClient = 1;
}
/ClientEndpointFactory.class
/WebSocketFactory.class
package example.websocket.factory;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.websocket.ClientEndpointConfig;
import javax.websocket.DeploymentException;
import javax.websocket.Endpoint;
import org.glassfish.tyrus.client.ClientManager;
import example.websocket.config.WebsocketConfig;
public class ClientEndpointFactory {
private static ClientEndpointFactory instance = null;
private ClientManager clientManager = ClientManager.createClient();
private ClientEndpointConfig cec = ClientEndpointConfig.Builder.create().build();
private ClientEndpointFactory(){
}
public static ClientEndpointFactory getInstance(){
if(instance == null){
instance = new ClientEndpointFactory();
}
return instance;
}
public void connect(Endpoint endpoint) throws DeploymentException, IOException, URISyntaxException{
clientManager.connectToServer(endpoint , cec, new URI(WebsocketConfig.URIPATH));
}
}
package example.websocket.factory;
import example.websocket.EndpointBase;
import example.websocket.WebsocketEndpoint;
public class WebSocketFactory {
private static WebSocketFactory instance;
private WebSocketFactory(){
}
public static WebSocketFactory getInstance(){
if(instance == null){
instance = new WebSocketFactory();
}
return instance;
}
public WebsocketEndpoint createEndPoint(Class<? extends EndpointBase> endpoint) throws InstantiationException, IllegalAccessException, ClassNotFoundException{
return endpoint.newInstance();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment