java webSocket
2021-03-07 08:29
标签:etl vol new 客户 cti error string too wired Springboot整合web socket 引入包 config server 到这里启动项目就行了 客户端就简单使用html F12查看 想要主动发送通知的话用下面的controller java webSocket 标签:etl vol new 客户 cti error string too wired 原文地址:https://www.cnblogs.com/Sora-L/p/14271601.html
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
* websocket配置
*
* @author luwl
* @date 2021/1/13 10:50
*/
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
import cn.com.zhengya.framework.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* @author luwl
* @date 2021/1/13 10:54
* @ServerEndpoint 注解是一个类层次的注解,它的功能主要是将目前的类定义成一个websocket服务器端,
* 注解的值将被用于监听用户连接的终端访问URL地址,客户端可以通过这个URL来连接到WebSocket服务器端
* {mac}是传递唯一标识的识别参数
*/
@Slf4j
@Component
@ServerEndpoint("/socket/{mac}")
public class WebSocketServer {
/**
* concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
*/
private static CopyOnWriteArraySet
DOCTYPE HTML>
html>
head>
meta charset="utf-8">
title>websocket 客户端title>
script type="text/javascript">
function WebSocketTest()
{
if ("WebSocket" in window)
{
alert("您的浏览器支持 WebSocket!");
// 创建一个 websocket
var ws = new WebSocket("ws://localhost:8034/socket/00149733FABB");
ws.onOpen = function()
{
// Web Socket 已连接上,使用 send() 方法发送数据
ws.send("发送数据");
console.log("数据发送中")
};
ws.onMessage = function (evt)
{
console.log(evt)
console.log(evt.data)
};
ws.onClose = function()
{
// 关闭 websocket
alert("连接已关闭...");
};
}
else
{
// 浏览器不支持 WebSocket
alert("您的浏览器不支持 WebSocket!");
}
}
script>
head>
body>
div id="sse">
a href="javascript:WebSocketTest()">运行 WebSocketa>
div>
body>
html>
import cn.com.zhengya.framework.model.Result;
import cn.com.zhengya.framework.socket.WebSocketServer;
import cn.com.zhengya.spdapi.dto.tool.socket.SocketParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* Socket 服务端
* @author luwl
* @date 2021/1/13 13:21
*/
@Slf4j
@RestController
@RequestMapping("/socket/api")
@Api(value = "socket服务", tags = "socket服务")
public class SocketController {
@Autowired
private WebSocketServer webSocketServer;
@PostMapping("/sendMessageToOne")
@ApiOperation(value = "发送消息给单个通道")
public Result> sendMessageToOne(@RequestBody SocketParam socketParam){
webSocketServer.sendMessage(socketParam.getMac(),socketParam.getMessage());
return Result.success();
}
@PostMapping("/sendMessageToALL")
@ApiOperation(value = "发送消息给所有通道")
public Result> sendMessageToAll(@RequestParam String message){
webSocketServer.sendMessageToAll(message);
return Result.success();
}
@PostMapping("/getAllSession")
@ApiOperation(value = "获取所有连接中通道")
public Result> getAllSession(){
return Result.success(webSocketServer.getAllMac());
}
}
/**
* @author luwl
* @date 2021/1/13 13:24
*/
@Data
@ApiModel("发送消息给指定的对象")
public class SocketParam {
@ApiModelProperty("发送对象标识(物理机器的MAC地址)")
private String mac;
@ApiModelProperty("消息")
private String message;
}