فهرست منبع

新增即时通讯相关代码

sjw 4 سال پیش
والد
کامیت
e50974329b

+ 230 - 0
src/dsb/com/cnd3b/common/websocket/WebClientSocket.java

@@ -0,0 +1,230 @@
+package com.cnd3b.common.websocket;
+
+import com.alibaba.fastjson.JSONObject;
+import com.cnd3b.common.BaseClass;
+import com.cnd3b.common.data.Row;
+import com.cnd3b.common.parameter.parameter;
+
+import javax.websocket.*;
+import javax.websocket.server.PathParam;
+import javax.websocket.server.ServerEndpoint;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Calendar;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+//ws://127.0.0.1:8080/samex/webSocket/829fef9884bbf7f9fb9c51499d7b332f
+@ServerEndpoint("/webSocket/{accesstoken}")
+public class WebClientSocket extends BaseClass {
+
+    //websocket连接池
+    public static Map<Long, WebClientSocket> websocketClients = new ConcurrentHashMap<Long, WebClientSocket>();
+    private Session session;
+    //当前连接对象的账号ID
+    private long userid;
+    private Row userRow;
+
+    @OnOpen
+    private void onOpen(@PathParam("accesstoken") String accesstoken, Session session) throws IOException {
+        this.session = session;
+        if (parameter.tokenlist.containsKey(accesstoken)) {
+            userid = parameter.tokenlist.get(accesstoken);
+            userRow = parameter.userIdList.get(userid);
+        } else {
+            sendMessage("请登陆!");
+        }
+        websocketClients.put(userid, this);
+    }
+
+    @OnClose
+    private void onClose() throws IOException {
+        websocketClients.remove(userid);
+    }
+
+    /**
+     * {
+     * "dialogid":1,//对话框id
+     * "messagetype":"text",//消息类型
+     * "description":"",//消息摘要
+     * "data":{
+     * "message":""//文字正文,
+     * }
+     * },
+     * {
+     * "dialogid":1,
+     * "messagetype":"file",//text,file,data,image,
+     * "description":"",
+     * "data":{
+     * "ownertable":"",
+     * "ownerid":"",
+     * "serialnumber":"",
+     * "postfix":"",
+     * "fobsurl":"",
+     * "fobsurl_minimage":"",
+     * "fobsurl_hls":"",
+     * "fdocument":""
+     * }
+     * },
+     * {
+     * "dialogid":1,
+     * "messagetype":"data",//text,file,data,image
+     * "description":"",
+     * "data":{
+     * "type":"",
+     * "ownertable":"",
+     * "ownerid":""
+     * }
+     * }
+     *
+     * @param messageObject
+     * @throws IOException
+     */
+    @OnMessage
+    private void onMessage(String messageObject) throws IOException {
+        JSONObject mesageObj = messageCheck(messageObject);
+        if (mesageObj == null) {
+            return;
+        }
+        long dialogid = mesageObj.getLong("dialogid");//对话框ID
+        /**
+         * 消息处理
+         */
+        mesageObj = messageProcessing(mesageObj);
+        /**
+         * 消息发送
+         */
+        sendMessageToDialog(mesageObj.toJSONString(), dialogid);
+    }
+
+    public JSONObject messageProcessing(JSONObject mesageObj) {
+        mesageObj.put("datetime", getDateTime_Str());
+        mesageObj.put("messageid", String.valueOf(userid) + Calendar.getInstance().getTimeInMillis());
+        /**
+         * 创建发送方信息
+         */
+        JSONObject sendfromObject = new JSONObject();
+        sendfromObject.put("userid", userid);
+        sendfromObject.put("siteid", userRow.getString("siteid"));
+        sendfromObject.put("username", userRow.getString("fname"));
+        mesageObj.put("sendfrom", sendfromObject);
+
+        /**
+         *  消息持久化,保存消息体mesageObj,字段:siteid,dialogid,userid,messageid,datetime,description,messagetype,mesageObj
+         */
+
+        return mesageObj;
+    }
+
+    /**
+     * 消息格式检查
+     *
+     * @param messageObject
+     * @throws Exception
+     */
+    public JSONObject messageCheck(String messageObject) {
+        JSONObject mesageObj = null;
+        String errmsg = "";
+        try {
+            mesageObj = JSONObject.parseObject(messageObject);
+        } catch (Exception e) {
+            errmsg = "不是有效的JSONObject消息格式";
+        }
+        if (!mesageObj.containsKey("dialogid") || mesageObj.getLongValue("dialogid") <= 0) {
+            errmsg = "dialogid缺失";
+        }
+        if (!mesageObj.containsKey("messagetype")) {
+            errmsg = "messagetype缺失";
+        }
+        String messagetype = mesageObj.getString("messagetype");
+        if (!Arrays.asList(new String[]{"text", "file", "data"}).contains(messagetype)) {
+            errmsg = "无效的messagetype";
+        }
+        if (!mesageObj.containsKey("description") || "".equals(mesageObj.getString("description"))) {
+            errmsg = "description缺失";
+        }
+        if (!mesageObj.containsKey("data")) {
+            errmsg = "data缺失";
+        }
+        JSONObject data = mesageObj.getJSONObject("data");
+        if ("text".equals(messagetype)) {
+            if (!data.containsKey("message") || "".equals(mesageObj.getString("message"))) {
+                errmsg = "text消息必须包含message";
+            }
+        } else if ("file".equals(messagetype)) {
+            if (!data.containsKey("ownertable") || "".equals(mesageObj.getString("ownertable"))) {
+                errmsg = "file消息必须包含ownertable";
+            }
+            if (!data.containsKey("ownerid") || "".equals(mesageObj.getString("ownerid"))) {
+                errmsg = "file消息必须包含ownerid";
+            }
+            if (!data.containsKey("serialnumber") || "".equals(mesageObj.getString("serialnumber"))) {
+                errmsg = "file消息必须包含serialnumber";
+            }
+            if (!data.containsKey("fobsurl") || "".equals(mesageObj.getString("fobsurl"))) {
+                errmsg = "file消息必须包含fobsurl";
+            }
+            if (!data.containsKey("fdocument") || "".equals(mesageObj.getString("fdocument"))) {
+                errmsg = "file消息必须包含fdocument";
+            }
+        } else if ("data".equals(messagetype)) {
+            if (!data.containsKey("type") || "".equals(mesageObj.getString("type"))) {
+                errmsg = "data消息必须包含type";
+            }
+            if (!data.containsKey("ownertable") || "".equals(mesageObj.getString("ownertable"))) {
+                errmsg = "data消息必须包含ownertable";
+            }
+            if (!data.containsKey("ownerid") || "".equals(mesageObj.getString("ownerid"))) {
+                errmsg = "data消息必须包含ownerid";
+            }
+        }
+        if (!"".equals(errmsg)) {
+            sendMessage(errmsg);
+            return null;
+        }
+        return mesageObj;
+    }
+
+    @OnError
+    private void onError(Session session, Throwable error) {
+        error.printStackTrace();
+    }
+
+
+    /**
+     * 对当前连接发送消息
+     *
+     * @param message
+     */
+    public void sendMessage(String message) {
+        session.getAsyncRemote().sendText(message);
+    }
+
+    /**
+     * 向指定的对象发送消息
+     *
+     * @param message
+     * @param toUserid
+     * @throws IOException
+     */
+    public void sendMessageToUser(String message, long toUserid) throws IOException {
+        if (websocketClients.containsKey(toUserid)) {
+            websocketClients.get(toUserid).session.getAsyncRemote().sendText(message);
+        }
+    }
+
+    /**
+     * 向指定的d对话框发送消息
+     *
+     * @param message
+     * @param dialogid
+     * @throws IOException
+     */
+    public void sendMessageToDialog(String message, long dialogid) throws IOException {
+        for (WebClientSocket item : websocketClients.values()) {
+            item.session.getAsyncRemote().sendText(message);
+        }
+    }
+
+
+}

+ 10 - 0
src/dsb/com/cnd3b/restcontroller/system/im/imdialog/SQL/即时通讯群人员添加.sql

@@ -0,0 +1,10 @@
+declare @timdialogusersid bigint
+set @timdialogusersid = (select isnull(MAX(timdialogusersid), 0) from timdialogusers)
+
+insert into timdialogusers
+(timdialogusersid, siteid, createby, changeby, createdate, changedate, rmkenable, fimdialognotes,fisremove,fname, tenterprise_userid,timdialogid)
+select @timdialogusersid + ROW_NUMBER() over (order by t1.fname),
+       t1.siteid, $createby$, $createby$, getdate(), getdate(), 0, null,0,t1.fname, t1.tenterprise_userid,$timdialogid$
+from tenterprise_users t1
+where t1.siteid=$siteid$ and t1.tenterprise_userid in $tenterprise_userid$
+and not exists(select * from timdialogusers where siteid=$siteid$ and timdialogid=$timdialogid$ and tenterprise_userid=t1.tenterprise_userid )

+ 95 - 0
src/dsb/com/cnd3b/restcontroller/system/im/imdialog/imdialog.java

@@ -0,0 +1,95 @@
+package com.cnd3b.restcontroller.system.im.imdialog;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.cnd3b.common.Controller;
+import com.cnd3b.common.D3bException;
+import com.cnd3b.common.data.SQLFactory;
+import p2.pao.PaoRemote;
+import p2.pao.PaoSetRemote;
+import p2.util.P2Exception;
+
+import java.util.ArrayList;
+
+public class imdialog extends Controller {
+
+    /**
+     * 构造函数
+     *
+     * @param content
+     */
+    public imdialog(JSONObject content) {
+        super(content);
+    }
+
+    /**
+     * 群对话框列表查询
+     *
+     * @return
+     */
+    public String query_imdialogList() {
+
+        return "";
+    }
+
+    /**
+     * 群对话框主界面查询
+     *
+     * @return
+     */
+    public String query_imdialogMain() {
+        return "";
+    }
+
+
+    public String insertOrModify() throws D3bException, P2Exception {
+        long timdialogid = content.getLongValue("timdialogid"); //群ID 新增时默认传0
+        String fimdialogname = content.getString("fimdialogname", "timdialog.fimdialogname", "群名称");//群名称
+        String fimdialogtype = content.getString("fimdialogtype");//对话类型
+        String ownertable = content.getString("ownertable");//所有表
+        String ownerid = content.getString("ownerid");//所有表ID
+
+        ArrayList<String> sqlList = new ArrayList<>();
+
+        PaoSetRemote timdialogSet = getP2ServerSystemPaoSet("timdialog", "siteid='" + siteid + "' and timdialogid='" + timdialogid + "'");
+        PaoRemote timdialog = null;
+        if (timdialogid <= 0 || timdialogSet.isEmpty()) {
+            /**
+             * 创建对话框
+             */
+            timdialog = timdialogSet.addAtEnd();
+            timdialog.setValue("siteid", siteid, 11L);//企业ID
+            timdialog.setValue("createby", username, 11L);//创建人
+            timdialog.setValue("createdate", sysdate, 11L);//创建时间
+            timdialog.setValue("fimdialogtype", fimdialogtype, 11L);//对话框类型
+            timdialog.setValue("ownertable", ownertable, 11L);//所有表
+            timdialog.setValue("ownerid", ownerid, 11L);//所有表ID
+            timdialog.setValue("tenterprise_userid", userid, 11L);//创建账号ID
+
+            timdialogid = timdialog.getUniqueIDValue();
+            /**
+             * 在初始对话框中添加人员
+             */
+            ArrayList<String> userlist = new ArrayList<>();
+
+            JSONArray usersArray = content.getJSONArray("users");
+            usersArray.add(userid);//默认将创建人员加入群
+            for (Object user : usersArray) {
+                userlist.add(String.valueOf(user));
+            }
+            SQLFactory sqlFactory = new SQLFactory(this, "即时通讯群人员添加");
+            sqlFactory.addParameter("createby", username);
+            sqlFactory.addParameter("timdialogid", timdialogid);
+            sqlFactory.addParameter("siteid", siteid);
+            sqlFactory.addParameter_in("tenterprise_userid", userlist);
+            sqlList.add(sqlFactory.getSQL());
+        } else {
+            timdialog = timdialogSet.getPao(0);
+        }
+        timdialog.setValue("fimdialogname", fimdialogname, 11L);//对话框类型
+        timdialogSet.save();
+        dbConnect.runSqlUpdate(sqlList);
+        content.put("timdialogid", timdialogid);
+        return query_imdialogMain();
+    }
+}