当前位置:网站首页 > Java基础 > 正文

java服务端基础知识



1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

java服务端基础知识

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

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

package com.mes.util;

 

import java.io.IOException;

import java.util.Map;

import java.util.concurrent.ConcurrentHashMap;

 

import javax.websocket.OnClose;

import javax.websocket.OnError;

import javax.websocket.OnMessage;

import javax.websocket.OnOpen;

import javax.websocket.Session;

import javax.websocket.server.PathParam;

import javax.websocket.server.ServerEndpoint;

 

import org.springframework.stereotype.Component;

import org.springframework.stereotype.Service;

 

import com.google.gson.JsonObject;

 

import net.sf.json.JSONObject;

@ServerEndpoint("/webSocket/{username}")  

    public class WebSocket { 

        private static int onlineCount = 0; 

        private static Map clients = new ConcurrentHashMap (); 

        private Session session; 

        private String username; 

           

        @OnOpen 

        public void onOpen(@PathParam("username") String username, Session session) throws IOException { 

       

            this.username = username; 

            this.session = session; 

               

            addOnlineCount(); 

            clients.put(username, this);

            System.out.println("已连接");

        } 

       

        @OnClose 

        public void onClose() throws IOException { 

            clients.remove(username); 

            subOnlineCount(); 

        } 

       

        @OnMessage 

        public void onMessage(String message) throws IOException { 

       

            JSONObject jsonTo = JSONObject.fromObject(message); 

            String mes = (String) jsonTo.get("message");

             

            if (!jsonTo.get("To").equals("All")){ 

                sendMessageTo(mes, jsonTo.get("To").toString()); 

            }else{ 

                sendMessageAll("给所有人"); 

            } 

        } 

       

        @OnError 

        public void onError(Session session, Throwable error) { 

            error.printStackTrace(); 

        } 

       

        public void sendMessageTo(String message, String To) throws IOException { 

            // session.getBasicRemote().sendText(message); 

            //session.getAsyncRemote().sendText(message); 

            for (WebSocket item : clients.values()) { 

                if (item.username.equals(To) ) 

                    item.session.getAsyncRemote().sendText(message); 

            } 

        } 

           

        public void sendMessageAll(String message) throws IOException { 

            for (WebSocket item : clients.values()) { 

                item.session.getAsyncRemote().sendText(message); 

            } 

        } 

       

        public static synchronized int getOnlineCount() { 

            return onlineCount; 

        } 

       

        public static synchronized void addOnlineCount() { 

            WebSocket.onlineCount++; 

        } 

       

        public static synchronized void subOnlineCount() { 

            WebSocket.onlineCount--; 

        } 

       

        public static synchronized Map getClients() { 

            return clients; 

        } 

版权声明


相关文章:

  • java基础整理笔记2024-11-12 11:18:02
  • java怎么学好基础2024-11-12 11:18:02
  • JAVA事件监听机制基础类2024-11-12 11:18:02
  • 叩丁狼教育java基础教程2024-11-12 11:18:02
  • 基础java计算器2024-11-12 11:18:02
  • 有java基础学框架2024-11-12 11:18:02
  • java0基础学习骗局2024-11-12 11:18:02
  • java基础集合的案例2024-11-12 11:18:02
  • java基础测试选择2024-11-12 11:18:02
  • 尚硅谷java基础笔记宋红康2024-11-12 11:18:02