Jetty使用WebSocket

package csm.web.controller;

import com.alibaba.fastjson.JSON;
import csm.app.page.ServerInterfaceImpl;
import csm.web.model.Net;
import csm.web.util.webThread.CacheTest;
import csm.web.util.webThread.DataQueue;
import csm.web.util.webThread.ReceiveSingle;
import csm.web.util.webThread.SendThread;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;


@ServerEndpoint("/webSocket/{userName}")
public class WebSocketDemo {

    private Session session;
    private String userName;
    private static int onlineCount = 0;//当前客户端的个数
    //客户端和对应的连接
    private static Map clients = new ConcurrentHashMap();
    //保存向页面推送的信息
    //连接建立
    @OnOpen
    public void onOpen(@PathParam("userName") String userName, Session session) {
        this.userName = userName;
        this.session = session;
        WebSocketDemo.onlineCount++;
        clients.put(userName, this);
        System.out.println("【websocket消息】有新的连接, 总数:{}==" + clients.toString());
    }

    //连接关闭
    @OnClose
    public void onClose() {
        clients.remove(userName);
        WebSocketDemo.onlineCount--;
        System.out.println("【websocket消息】连接断开, 总数:{}==" + clients.toString());
    }

    //客户端发送的信息
    @OnMessage
    public void onMessage(String message) {
        System.out.println("【websocket消息】收到客户端发来的消息:{}==" + message);
    }

    /**
     * 推送消息到客户端
     *
     * @throws IOException
     */
    public static void sendMessage() {
        try {
            Map map = DataQueue.getMagQueue().take();
            String datakey = map.keySet().iterator().next();
            Iterator iterator = clients.keySet().iterator();
            while (iterator.hasNext()){
                String name=iterator.next();
                if (name.contains(datakey) && datakey.equals("netNode")) {
                    WebSocketDemo webSocket = clients.get(name);
                    List> list = (List>) map.get(datakey);
                    String string = JSON.toJSONString(list);
                    webSocket.session.getBasicRemote().sendObject(string);
                }else if (name.contains(datakey) && datakey.equals("file")){
                    WebSocketDemo webSocket = clients.get(name);
                    webSocket.session.getBasicRemote().sendObject(map.get(datakey).toString());
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (EncodeException e) {
            System.out.println(234);
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println(1231);
        }
    }
}

pom


      org.eclipse.jetty.websocket
      websocket-client
      ${jetty.version}
    
    
      org.eclipse.jetty.websocket
      websocket-server
      ${jetty.version}
    
    
      org.eclipse.jetty.websocket
      websocket-api
      ${jetty.version}
    
    
      org.eclipse.jetty.websocket
      javax-websocket-server-impl
      ${jetty.version}
    

jetty启动类

ServerContainer serverContainer = WebSocketServerContainerInitializer.configureContext((ServletContextHandler) server.getHandler());
            serverContainer.addEndpoint(WebSocketDemo.class);

 

你可能感兴趣的:(java)