当前位置:   article > 正文

java实现WebSocket服务端_org.java-websocket:java-websocket

org.java-websocket:java-websocket

三个类,简单实现WebSocket服务端

-首先引入jar包

<!-- https://mvnrepository.com/artifact/org.java-websocket/Java-WebSocket -->
        <dependency>
            <groupId>org.java-websocket</groupId>
            <artifactId>Java-WebSocket</artifactId>
            <version>1.3.7</version>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

MsgWebSocketServer


import java.net.InetSocketAddress;
import java.util.Iterator;

import com.alibaba.fastjson.JSONObject;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;


/**
 * websocket服务器
 */
public class MsgWebSocketServer extends WebSocketServer{

    public MsgWebSocketServer(int port) {
        super(new InetSocketAddress(port));
    }
    /**
     * WebSocket连接关闭时调用
     */
    @Override
    public void onClose(WebSocket ws, int arg1, String arg2, boolean arg3) {
        System.out.println("------------------onClose-------------------");
    }

    /**
     * 错误发生时调用。
     */
    @Override
    public void onError(WebSocket ws, Exception e) {
        System.out.println("------------------onError-------------------");
        if(ws != null) {
        }
        e.getStackTrace();
    }

    /**
     * 接收到的消息
     */
    @Override
    public void onMessage(WebSocket ws, String msg) {
        System.out.println("收到消息:"+msg);
        //收到什么消息,回复什么
        ws.send(msg);
        if(ws.isClosed()) {
        } else if (ws.isClosing()) {
            System.out.println("ws连接正在关闭...");
        } else if (ws.isConnecting()) {
            System.out.println("ws正在连接中...");
        } else if(ws.isOpen()) {
            System.out.println("ws连接已打开...");
            System.out.println(msg);
        }
    }

    /**
     * websocket进行握手之后调用,并且给WebSocket写做准备
     * 通过握手可以获取请求头信息
     */
    @Override
    public void onOpen(WebSocket ws, ClientHandshake shake) {
        System.out.println("-----------------onOpen--------------------"+ws.isOpen()+"--"+ws.getReadyState()+"--"+ws.getAttachment());
        for(Iterator<String> it=shake.iterateHttpFields();it.hasNext();) {
            String key = it.next();
            System.out.println(key+":"+shake.getFieldValue(key));
        }
    }
    /**
     * 当服务器成功启动时调用
     */
    @Override
    public void onStart() {
        System.out.println("------------------onStart-------------------");
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77

WebServerEnum


public enum WebServerEnum {

    server;

    private static MsgWebSocketServer socketServer = null;

    public static void init(MsgWebSocketServer server) {
        socketServer = server;
        if (socketServer != null) {
            socketServer.start();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

启动类

public class SocketServerEngine {
    public static void main(String[] args) {
        WebServerEnum.server.init(new MsgWebSocketServer(8090));
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5

连接地址: ws://localhost:8090

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/120087
推荐阅读
相关标签
  

闽ICP备14008679号