Browse Source

add:添加socket打印

master
zhangzq 4 weeks ago
parent
commit
e0d9e1ad27
  1. 107
      acs/nladmin-system/nlsso-server/src/main/java/org/nl/common/mnt/websocket/SendPrintSocket.java
  2. 26
      acs/nladmin-ui/src/views/acs/order/template.vue

107
acs/nladmin-system/nlsso-server/src/main/java/org/nl/common/mnt/websocket/SendPrintSocket.java

@ -0,0 +1,107 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.nl.common.mnt.websocket;
import com.alibaba.fastjson.JSON;
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.util.concurrent.CopyOnWriteArraySet;
/*
* @author ZZQ
* @Date 2024/1/8 17:03
*/
@ServerEndpoint("/webSocket/sendPrint/{sid}")
@Slf4j
@Component
public class SendPrintSocket {
/**
* concurrent包的线程安全Set用来存放每个客户端对应的MyWebSocket对象
*/
private static CopyOnWriteArraySet<SendPrintSocket> webSocketSet = new CopyOnWriteArraySet<SendPrintSocket>();
/**
* 与某个客户端的连接会话需要通过它来给客户端发送数据
*/
private Session session;
/**
* 接收sid
*/
private String sid = "";
/**
* 连接建立成功调用的方法
*/
@OnOpen
public void onOpen(Session session, @PathParam("sid") String sid) {
this.session = session;
//如果存在就先删除一个,防止重复推送消息
for (SendPrintSocket webSocket : webSocketSet) {
if (webSocket.sid.equals(sid)) {
webSocketSet.remove(webSocket);
}
}
webSocketSet.add(this);
this.sid = sid;
}
/**
* 连接关闭调用的方法
*/
@OnClose
public void onClose() {
webSocketSet.remove(this);
}
/**
* 收到客户端消息后调用的方法
*
* @param message 客户端发送过来的消息
*/
@OnMessage
public void onMessage(String message, Session session) {
System.out.println(webSocketSet.size()+"_接收到消息_"+session.getId());
}
@OnError
public void onError(Session session, Throwable error) {
log.error("发生错误");
webSocketSet.remove(session);
error.printStackTrace();
}
public static void sendPrint(String print){
if (webSocketSet.size()>0){
try {
//如果有多个客户端连接一个服务端,只要给一个客户端下发打印即可
for (SendPrintSocket printSocket : webSocketSet) {
if (printSocket.session.isOpen()){
printSocket.session.getBasicRemote().sendText(print);
break;
}
}
}catch (Exception ex){
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
SendPrintSocket.sendPrint("xxx");
}
}

26
acs/nladmin-ui/src/views/acs/order/template.vue

@ -269,6 +269,7 @@ import $ from 'jquery'
import { hiprint, defaultElementTypeProvider } from 'vue-plugin-hiprint' import { hiprint, defaultElementTypeProvider } from 'vue-plugin-hiprint'
import template from '@/api/acs/order/template' import template from '@/api/acs/order/template'
import Preview from '@/views/acs/order/preview.vue' import Preview from '@/views/acs/order/preview.vue'
import uuidv1 from 'uuid/v1'
// provider // provider
hiprint.init({ hiprint.init({
@ -349,14 +350,29 @@ export default {
}) })
}, },
created() { created() {
template.getTemplateList().then(data => { template.getTemplateList().then(data => {
this.templateList = data this.templateList = data
}) })
template.getTicketList().then(data => { template.getTicketList().then(data => {
this.ticketList = data this.ticketList = data
})
if (typeof (WebSocket) === 'undefined') {
this.$notify({
title: '提示',
message: '当前浏览器无法接收实时打印信息,请使用谷歌浏览器!',
type: 'warning',
duration: 0
}) })
}
const wsUri = window.g.prod.VUE_APP_BASE_API.replace('http', 'ws') + '/webSocket/sendPrint/' + uuidv1()
this.websock = new WebSocket(wsUri)
this.websock.onmessage = this.webSocketOnMessage
}, },
methods: { methods: {
webSocketOnMessage(e) {
console.log('-----获取到消息------')
console.log(e)
},
addTable() { addTable() {
this.formDias = true this.formDias = true
this.isDisabled = false this.isDisabled = false

Loading…
Cancel
Save