跳到主要内容

websocket

架构概览

image-20220728234101270

  • websocket的session的唯一性要通过userId 来保证,多端(买家卖家管理端)情况下 userId可能会重复,所以需要通过apptype(admin,seller,buyer)来隔离
  • ui和websocket建立连接时需要传递apptype和token参数,token会校验、解析出userId

规范

建立连接

连接地址

ws://{ip}/ws/shop

参数

参数名类型说明
appType字串Buyer("买家端"), Admin("平台管理端"), Shop("门店管理端")
token字串登录后发放的access_token

消息发送

接口

com.enation.app.javashop.framework.ws.MessagePublisher

参数

Message:

属性类型说明
appType字串Buyer("买家端"), Admin("平台管理端"), Shop("门店管理端")
userIdLong用户id
moduleType字串模块类型:见模块类型对照表
data字串要发送的数据

模块类型对照表

类型对应模块
SpuImportspu数据导入
SkuImportsku数据导入
ParameterImport参数数据导入
GalleryImport相册数据导入
GoodsDetailImport商品详情数据导入
IMIm消息

消息接收

参数规范

{
"app_type": "Admin",
"module_type": "import",
"user_id": 123,
"data": "hello 123",
"msg_time": 1659085305
}

参数说明

属性类型说明
app_type字串Buyer("买家端"), Admin("平台管理端"), Shop("门店管理端")
user_idLong用户id
module_type字串模块类型:见下表
data字串要发送的消息

module_type说明

说明
SpuImportspu导入
SkuImportsku导入
ParameterImport参数导入
GalleryImport相册导入
GoodsDetailImport商品详情导入
IMIM消息

示例

UI端建立链接

var wsurl= "ws://wshost/ws/shop?token=xxx&appType=Admin";
var websocket = new ReconnectingWebSocket(wsurl);

websocket.onopen = function(event){
console.log('建立连接')
}

服务器端发送消息

@Autowired
private MessagePublisher messagePublisher;

Message message = new Message();
message.setAppType(AppTypeEnum.Admin);
message.setUserId(123L);
message.setMsg("hello 123");
message.setModuleType("import");

messagePublisher.sendMessage(message);

UI端接收消息

 websocket.onmessage = function(event){
console.log('收到消息:'+event.data)
}