Live online chat with Activemq, Stomp, SOCKJS
ActiveMQ: Powerful open source instant Messaging and Integrated mode servers. In this project, act as the message Proxy Server, STOMP Protocol service side.
Install: Download on official website, unzip directly, run CMD, go to Bin directory to execute Activemq.bat start;linux./activemq start.
Stomp:stomp is a text-directed communication protocol. This project uses the Stomp protocol based JavaScript client library Stomp.js
Install: Download stomp.js, introduce in project
SOCKJS:Sockjs is WebSocket's JavaScript library, is the implementation of WebSocket
Install: Download sockjs.js, introduce in project
The first step: Create a Javaweb project, configure MAVEN dependencies, and rely on the following
1 <Properties>2 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>3 <spring.version>4.2.4.RELEASE</spring.version>4 </Properties>5 6 <Dependencies>7 <Dependency>8 <groupId>Org.springframework</groupId>9 <Artifactid>Spring-webmvc</Artifactid>Ten <version>${spring.version}</version> One </Dependency> A - <Dependency> - <groupId>Org.springframework</groupId> the <Artifactid>Spring-websocket</Artifactid> - <version>${spring.version}</version> - </Dependency> - + <Dependency> - <groupId>Org.springframework</groupId> + <Artifactid>Spring-messaging</Artifactid> A <version>${spring.version}</version> at </Dependency> - - <Dependency> - <groupId>Io.projectreactor</groupId> - <Artifactid>Reactor-net</Artifactid> - <version>2.0.7.RELEASE</version> in </Dependency> - to <Dependency> + <groupId>Io.netty</groupId> - <Artifactid>Netty-all</Artifactid> the <version>4.0.33.Final</version> * </Dependency> $ Panax Notoginseng - <Dependency> the <groupId>Com.fasterxml.jackson.core</groupId> + <Artifactid>Jackson-databind</Artifactid> A <version>2.6.4</version> the </Dependency> + - <Dependency> $ <groupId>Javax.servlet</groupId> $ <Artifactid>Javax.servlet-api</Artifactid> - <version>3.1.0</version> - <Scope>Provided</Scope> the </Dependency> - Wuyi the </Dependencies>
Step Two: Configure the message terminal to include the following configuration in the Springmvc configuration file
<Websocket:message-brokerApplication-destination-prefix= "/app"> <Websocket:stomp-endpointPath= "/stomp"> <Websocket:sockjs/> </Websocket:stomp-endpoint> <Websocket:stomp-broker-relayprefix= "/topic,/queue"Relay-host= "localhost"Relay-port= "61613"Heartbeat-receive-interval= "20000"Heartbeat-send-interval= "20000" /> </Websocket:message-broker>
Step three: Write Message processing code
PackageCom.its.controller;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.messaging.handler.annotation.MessageMapping;Importorg.springframework.messaging.simp.SimpMessagingTemplate;ImportOrg.springframework.stereotype.Controller; @Controller Public classMessagecontroller {@AutowiredPrivatesimpmessagingtemplate template; @MessageMapping ("Channel") Publicstring Send (String message) {string text=message; Template.convertandsend ("/topic/pinkzhuang", text); returntext; }}
Fourth step: Write the page message transceiver logic, the page to introduce Stomp.js and sockjs.js
functionConnect () {varSocket =NewSOCKJS ("Http://localhost:8080/ActivityMQStomp/stomp"); Stompclient=stomp.over (socket); Stompclient.connect ({},function(frame) {setconnected (true); Console.log ("Connected:" +frame); Stompclient.subscribe ("/topic/pinkzhuang",function(greeting) {showgreeting (greeting.body); }); }); }functionDisconnect () {stompclient.disconnect ();}functionsendMessage (message) {Stompclient.send ("/app/vince", {},name);}
Fifth step: Can login localhost:8161 to enter ACTIVEMQ Management page, the initial account password is admin, you can manually send messages on the Administration page
The End.
ActiveMQ, Stomp, SOCKJS entry-level applications