首页 > 编程知识 正文

做推送的软件,websocket应用场景

时间:2023-05-05 19:13:11 阅读:163885 作者:1876

控制器:

package com.example.demo.controller;

importorg.spring framework.messaging.handler.annotation.message mapping;

importorg.spring framework.messaging.handler.annotation.send to;

importorg.spring framework.stereotype.controller;

importorg.spring framework.web.bind.annotation.request mapping;

importorg.spring framework.web.bind.annotation.response body;

import com.example.demo.bean.in message;

import com.example.demo.bean.out message;

@Controller

公共类图形信息控制器{

@messagemapping(/v1/chat ) )。

@sendto(/topic/game_chat ) )。

publicoutmessagegameinfo (inmessagemessage ) (

returnnewoutmessage (message.get content );

}

}

=========================================================================================

package com.example.demo.config;

importorg.spring framework.context.annotation.configuration;

importorg.spring framework.messaging.simp.config.messagebrokerregistry;

importorg.spring framework.web.socket.config.annotation .冷静的黑米;

importorg.spring framework.web.socket.config.annotation.enablewebsocketmessagebroker;

importorg.spring framework.web.socket.config.annotation.stompendpointregistry;

@Configuration

@EnableWebSocketMessageBroker

publicclasswebsocketconfigextends冷静的黑米{

//*

publicvoidregisterstompendpoints (stompendpointregistryregistry ) (

registry.add endpoint ((/endpoint-web socket ) ).setallowedorigins ).withSockJS );

}

//*

@Override

publicvoidconfiguremessagebroker (消息brokerregistryregistry ) {

registry.enablesimplebroker ('/topic ','/chat ' );

registry.setapplicationdestinationprefixes ()/app );

}

}

=========================================================================================

! DOCTYPE html

html

meta http-equiv=' content-type ' content=' text/html; charset=utf-8'/

头儿

titleHello WebSocket/title

script src=' jquery-3.2.1.min.js '/script

script src=' sock js.min.js '/script

script src='stomp.js'/script

script src='app.js'/script

/head

身体

noscript H2 style=' color : # ff 0000 ' seemsyourbrowserdoesn ' tsupportjavascript! websocketreliesonjavascriptbeing

enabled. Please enable

Javascript and reload this page! /h2/noscript

divid='主内容' class=' container '

div class='row '

div class='col-md-12 '

table id=' conversation ' class=' table table-striped '

泰德

tr

th游戏公告内容/th

/tr

/thead

tbody id='notice '

/tbody

/table

/div

/div

/div

/body

/html

=========================================================================================

var stompClient=null;

函数连接(

var socket=new sockjs (/endpoint-web socket ); //连接到端点(基站)

stompclient=stomp.over(socket ); 用stom包装,规范协议

stompClient.connect({},function (frame

console.log (连接: ) frame;

stomp client.subscribe ('/topic/game _ chat ',function({

console.info(result )

showcontent(JSON.parse ) result.body );

);

);

}

函数显示内容(body ) {

$('#notice ' ).append (' trtd ' body.content '/tdtd ' new date (body.time ).toLocaleString ) )/td/tr

}

$(function () )。

连接(;

);

=========================================================================================

stompclient.send((/app/V1/chat )、)、JSON.stringify )、content(:(789451230 ) );

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。