腾讯T2亲自讲解,【计算机网络 11

package servlet.websocket;

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 servlet.Log;

/**

  • websocket服务端

  • @author leibf

*/

@ServerEndpoint(value = “/websocket/{clientId}”)

public class WebSocketServer {

private final Log log = new Log(WebSocketServer.class);

private Session session;

private String clientId;

private static Map<String, WebSocketServer> clients = new ConcurrentHashMap<String, WebSocketServer>();



// 连接时执行

@OnOpen

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

	this.session = session;

	this.clientId = clientId;

	clients.put(clientId, this);

	log.info("新连接:" + clientId);

}



// 关闭时执行

@OnClose

public void onClose(@PathParam("clientId") String clientId, Session session) {

	clients.remove(clientId);



	log.info("连接 " + clientId + " 关闭");

}



// 收到消息时执行

@OnMessage

public void onMessage(String message, Session session) throws IOException {

	log.info("收到用户的消息: "+  message);

	/*if("getMpDefsAndRtDatas".equals(message)){

		String msg = UnityServlet.getInstance().getAllMpDefsAndRtDatas();

		this.sendMessage(session, msg);

	}*/

}



// 连接错误时执行

@OnError

public void onError(@PathParam("clientId") String clientId, Throwable error, Session session) {

	log.info("用户id为:" + clientId + "的连接发送错误");

	error.printStackTrace();

}



/**

 * 发送消息给某个客户端

 * @param message

 * @param To

 * @throws IOException

 */

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

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

		if (item.clientId.equals(To))

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

	}

}



/**

 * 发送消息给某些客户端

 * @param message

 * @param To

 * @throws IOException

 */

public static void sendMessageToSomeone(String message, String To) throws IOException {

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

		if (item.clientId.startsWith(To))

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

	}

}



/**

 * 发送消息给所有客户端

 * @param message

 * @throws IOException

 */

public static void sendMessageAll(String message) throws IOException {

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

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

	}

}



/**

 * 发送消息

 * @param session

 * @param message

 * @throws IOException

 */

private void sendMessage(Session session,String message) throws IOException{

    session.getBasicRemote().sendText(message);

}

}




Java端发送请求指令



String clientId = “broadcast”;

try {

WebSocketServer.sendMessageTo("broadcast",clientId);

} catch (IOException e) {

e.printStackTrace();

结语

小编也是很有感触,如果一直都是在中小公司,没有接触过大型的互联网架构设计的话,只靠自己看书去提升可能一辈子都很难达到高级架构师的技术和认知高度。向厉害的人去学习是最有效减少时间摸索、精力浪费的方式。

我们选择的这个行业就一直要持续的学习,又很吃青春饭。

虽然大家可能经常见到说程序员年薪几十万,但这样的人毕竟不是大部份,要么是有名校光环,要么是在阿里华为这样的大企业。年龄一大,更有可能被裁。

小编整理的学习资料分享一波!

送给每一位想学习Java小伙伴,用来提升自己。想要资料的可以点击这里免费获取
在这里插入图片描述

方式。

我们选择的这个行业就一直要持续的学习,又很吃青春饭。

虽然大家可能经常见到说程序员年薪几十万,但这样的人毕竟不是大部份,要么是有名校光环,要么是在阿里华为这样的大企业。年龄一大,更有可能被裁。

小编整理的学习资料分享一波!

送给每一位想学习Java小伙伴,用来提升自己。想要资料的可以点击这里免费获取
[外链图片转存中…(img-3Yy5EsEm-1628235688723)]

本文到这里就结束了,喜欢的朋友可以帮忙点赞和评论一下,感谢支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值