All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.xishan.oftenporter.bridge.http.websocket.WSClient Maven / Gradle / Ivy

Go to download

转接远程的http接口,服务器响应正确的数据格式必须是JResponse定义的格式。 客户端websocket使用"org.java-websocket:Java-WebSocket:1.5.2",项目地址https://github.com/TooTallNate/Java-WebSocket; 对Java-WebSocket做了适当修改。

The newest version!
package cn.xishan.oftenporter.bridge.http.websocket;


import java.io.IOException;

/**
 * @author Created by https://github.com/CLovinr on 2017/10/12.
 */
public class WSClient
{
    ClientWebSocket.Type type;
    SessionImpl session;
    Object object;

    /**
     * 返回值类型:
     * 
    *
  1. * {@linkplain ClientWebSocket.Type#ON_OPEN}:null *
  2. *
  3. * {@linkplain ClientWebSocket.Type#ON_MESSAGE}:String *
  4. *
  5. * {@linkplain ClientWebSocket.Type#ON_ERROR}:Throwable *
  6. *
  7. * {@linkplain ClientWebSocket.Type#ON_CLOSE}:{@linkplain ClientCloseReason} *
  8. *
  9. * {@linkplain ClientWebSocket.Type#ON_PONG}:{@linkplain java.nio.ByteBuffer} *
  10. *
  11. * {@linkplain ClientWebSocket.Type#ON_BINARY_BYTE_BUFFER}:{@linkplain java.nio.ByteBuffer} *
  12. *
* * @param * @return */ public T object() { return (T) object; } public Session session() { return session; } public ClientWebSocket.Type type() { return type; } WSClient() { } void setSession(SessionImpl session) { this.session = session; } void set(ClientWebSocket.Type type, Object object) { this.type = type; this.object = object; } public void close() throws IOException { close(null); } public void close(ClientCloseReason closeReason) throws IOException { if (session == null) { return; } if (closeReason != null) { session.close(closeReason.getCode(), closeReason.getReason()); } else { session.close(); } } @Override public boolean equals(Object obj) { if (!(obj instanceof WSClient)) { return false; } else { WSClient ws = (WSClient) obj; return ws.session.getId().equals(session.getId()); } } @Override public int hashCode() { return session.getId().hashCode(); } @Override public String toString() { String builder = getClass().getName() + "@" + super.hashCode() + "-->" + session.getClass().getSimpleName() + "@" + session.hashCode() + ",id=" + session.getId(); return builder; } public final T removeAttribute(Class clazzKey) { return removeAttribute(clazzKey.getName()); } public final T removeAttribute(String key) { return (T) session.getUserProperties().remove(key); } public final T putAttribute(Class clazzKey, Object value) { return putAttribute(clazzKey.getName(), value); } /** * @return 返回上一次的属性。 */ public final T putAttribute(String key, Object value) { return (T) session.getUserProperties().put(key, value); } public final T getAttribute(Class clazzKey) { return getAttribute(clazzKey.getName()); } public final T getAttribute(String key) { Object v = session.getUserProperties().get(key); return (T) v; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy