Skip to content

Commit

Permalink
100 added websocket working examples
Browse files Browse the repository at this point in the history
  • Loading branch information
iuliana committed Jul 29, 2017
1 parent 039f5d9 commit 98bc41a
Show file tree
Hide file tree
Showing 44 changed files with 837 additions and 301 deletions.
20 changes: 13 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ ext {
bootstrapVersion = '3.3.7-1'
thymeSecurityVersion = '3.0.2.RELEASE'
jQueryVersion = '3.2.1'
twsVersion = '9.0.0.M22'

//persistency libraries
hibernateVersion = '5.2.10.Final'
Expand Down Expand Up @@ -65,7 +66,9 @@ ext {
securityConfig : "org.springframework.security:spring-security-config:$springSecurityVersion",
securityTaglibs : "org.springframework.security:spring-security-taglibs:$springSecurityVersion",
rabbit : "org.springframework.amqp:spring-rabbit:$springAmqpVersion",
springSecurityTest: "org.springframework.security:spring-security-test:$springSecurityVersion"
springSecurityTest: "org.springframework.security:spring-security-test:$springSecurityVersion",
webSocket : "org.springframework:spring-websocket:$springVersion",
messaging : "org.springframework:spring-messaging:$springVersion"

]

Expand Down Expand Up @@ -110,6 +113,7 @@ ext {
dbunit : "org.dbunit:dbunit:$dbunitVersion"
]


misc = [
slf4jJcl : "org.slf4j:jcl-over-slf4j:$slf4jVersion",
logback : "ch.qos.logback:logback-classic:$logbackVersion",
Expand All @@ -125,24 +129,26 @@ ext {
artemis : "org.apache.activemq:artemis-jms-server:2.1.0",
javaEl : "javax.el:javax.el-api:$javaElVersion",
glassfishEl : "org.glassfish.web:el-impl:$glasshfishELVersion",
servlet : "javax.servlet:javax.servlet-api:3.1.0",
jms : "javax.jms:javax.jms-api:$jmsVersion",
artemisClient : "org.apache.activemq:artemis-jms-client:$artemisVersion",
artemisServer : "org.apache.activemq:artemis-jms-server:$artemisVersion",
hornetq : "org.hornetq:hornetq-jms-client:$hornetqVersion",
castor : "org.codehaus.castor:castor-xml:$castorVersion",
jacksonDatabind: "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion",
httpclient : "org.apache.httpcomponents:httpclient:$httpclientVersion",
poi : "org.apache.poi:poi:$poiVersion",
io : "commons-io:commons-io:2.5",
tiles : "org.apache.tiles:tiles-jsp:$tilesVersion",
jstl : "jstl:jstl:1.2"
]

web = [
tiles : "org.apache.tiles:tiles-jsp:$tilesVersion",
jstl : "jstl:jstl:1.2",
bootstrap : "org.webjars:bootstrap:$bootstrapVersion",
jQuery : "org.webjars:jquery:$jQueryVersion",
thymeSecurity: "org.thymeleaf.extras:thymeleaf-extras-springsecurity4:$thymeSecurityVersion"
thymeSecurity: "org.thymeleaf.extras:thymeleaf-extras-springsecurity4:$thymeSecurityVersion",
tomcatWsApi : "org.apache.tomcat:tomcat-websocket-api:$twsVersion",
tomcatWsEmbed: "org.apache.tomcat.embed:tomcat-embed-websocket:$twsVersion",
httpclient : "org.apache.httpcomponents:httpclient:$httpclientVersion",
websocket : "javax.websocket:javax.websocket-api:1.1",
servlet : "javax.servlet:javax.servlet-api:3.1.0",
]

db = [
Expand Down
1 change: 0 additions & 1 deletion chapter12/chapter12.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ subprojects {
//we specify these dependencies for all submodules, except the boot module, that defines its own
if (!project.name.contains("boot")) {
compile (spring.contextSupport) {
exclude module: 'spring-context'
exclude module: 'spring-beans'
exclude module: 'spring-core'
}
Expand Down
2 changes: 1 addition & 1 deletion chapter12/rest/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'war'

dependencies {
compile project(':chapter12:base-remote')
compile spring.oxm, spring.webmvc, misc.servlet, misc.castor, misc.jacksonDatabind, misc.httpclient
compile spring.oxm, spring.webmvc, web.servlet, misc.castor, misc.jacksonDatabind, web.httpclient
testCompile spring.test
}

Expand Down
2 changes: 1 addition & 1 deletion chapter12/secure-rest/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'war'

dependencies {
compile project(':chapter12:base-remote')
compile spring.oxm, spring.webmvc, misc.servlet, misc.castor, misc.jacksonDatabind, misc.httpclient,
compile spring.oxm, spring.webmvc, web.servlet, misc.castor, misc.jacksonDatabind, web.httpclient,
spring.securityWeb, spring.securityConfig
testCompile spring.test
}
Expand Down
2 changes: 1 addition & 1 deletion chapter12/spring-invoker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'war'

dependencies {
compile project(':chapter12:base-remote')
compile spring.webmvc, misc.servlet
compile spring.webmvc, web.servlet
testCompile spring.test
}

Expand Down
4 changes: 2 additions & 2 deletions chapter15/chapter15.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ subprojects {
exclude module: 'spring-core'
}
compile misc.slf4jJcl, misc.logback, misc.lang3, hibernate.em, hibernate.jpaApi, misc.guava, db.h2,
spring.data, spring.webmvc, misc.servlet, spring.oxm, misc.castor,
misc.jacksonDatabind, misc.httpclient
spring.data, spring.webmvc, web.servlet, spring.oxm, misc.castor,
misc.jacksonDatabind, web.httpclient
}
testCompile testing.junit
}
Expand Down
4 changes: 2 additions & 2 deletions chapter16/chapter16.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ subprojects {
exclude module: 'spring-core'
}
compile misc.slf4jJcl, misc.logback, misc.lang3, hibernate.em, hibernate.validator, db.h2,
spring.data, spring.webmvc, misc.castor, misc.validation, misc.tiles, misc.jstl,
misc.jacksonDatabind, misc.servlet, misc.io
spring.data, spring.webmvc, misc.castor, misc.validation, web.tiles, web.jstl,
misc.jacksonDatabind, web.servlet, misc.io
}
testCompile testing.junit
}
Expand Down
12 changes: 11 additions & 1 deletion chapter17/chapter17.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ subprojects {
}

dependencies {
compile spring.webmvc, misc.slf4jJcl, misc.logback, misc.lang3
if (!project.name.contains("boot")) {
compile (web.tomcatWsApi) {
exclude module: 'tomcat-embed-core'
}
compile (web.tomcatWsEmbed) {
exclude module: 'tomcat-embed-core'
}
compile spring.context, spring.webSocket, spring.messaging, spring.webmvc, web.websocket, misc.slf4jJcl,
misc.logback, misc.lang3, misc.jacksonDatabind, web.servlet, web.jstl
}
testCompile testing.junit
}
}
10 changes: 10 additions & 0 deletions chapter17/sockjs-cfg/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apply plugin: 'war'

war {
archiveName = 'sockjs2.war'
manifest {
attributes("Created-By" : "Iuliana Cosmina",
"Specification-Title": "Pro Spring 5",
"Class-Path" : configurations.compile.collect { it.getName() }.join(' '))
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.apress.prospring4.ch17;
package com.apress.prospring5.ch17;

import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.apress.prospring5.ch17.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
* Created by iuliana.cosmina on 7/29/17.
*/
@Configuration
@EnableWebMvc
@EnableAsync
@ComponentScan(basePackages = {"com.apress.prospring5.ch17"})
public class WebConfig implements WebMvcConfigurer {

// <=> <mvc:default-servlet-handler/>
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}

// <=> <mvc:view-controller .../>
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("/static/index.html");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.apress.prospring5.ch17.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{
WebSocketConfig.class
};
}

@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{
WebConfig.class
};
}

@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.apress.prospring5.ch17.config;

import com.apress.prospring5.ch17.EchoHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.*;

/**
* Created by iuliana.cosmina on 7/29/17.
*/
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(echoHandler(), "/echoHandler").withSockJS();
}

@Bean
public EchoHandler echoHandler() {
return new EchoHandler();
}
}
96 changes: 96 additions & 0 deletions chapter17/sockjs-cfg/src/main/webapp/static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<html>
<head>
<meta charset="UTF-8">
<title>SockJS Tester</title>
<script language="javascript" type="text/javascript" src="https://d1fxtkz8shb9d2.cloudfront.net/sockjs-0.3.4.min.js"></script>
<script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script language="javascript" type="text/javascript">
var ping;
var sockjs;

jQuery(function ($) {
function writePing(message) {
$('#pingOutput').append(message + '\n');
}

function writeStatus(message) {
$("#statusOutput").val($("#statusOutput").val() + message + '\n');
}

function writeMessage(message) {
$('#messageOutput').append(message + '\n')
}

$('#connect')
.click(function doConnect() {
sockjs = new SockJS($("#target").val());

sockjs.onopen = function (evt) {
writeStatus("CONNECTED");

var ping = setInterval(function () {
if (sockjs != "undefined") {
sockjs.send("ping");
}
}, 3000);
};

sockjs.onclose = function (evt) {
writeStatus("DISCONNECTED");
};

sockjs.onmessage = function (evt) {
if (evt.data === "ping") {
writePing(evt.data);
} else {
writeMessage('ECHO: ' + evt.data);
}
};

sockjs.onerror = function (evt) {
onError(writeStatus('ERROR:' + evt.data))
};
});

$('#disconnect')
.click(function () {
if(typeof sockjs != 'undefined') {
sockjs.close();
sockjs = undefined;
} else {
alert("Not connected.");
}
});

$('#send')
.click(function () {
if(typeof sockjs != 'undefined') {
sockjs.send($('#message').val());
} else {
alert("Not connected.");
}
});
});
</script>
</head>

<body>
<h2>SockJS Tester</h2>
Target:
<input id="target" size="40" value="http://localhost:8080/sockjs/echoHandler"/>
<br/>
<button id="connect">Connect</button>
<button id="disconnect">Disconnect</button>
<br/>
<br/>Message:
<input id="message" value=""/>
<button id="send">Send</button>
<br/>
<p>Status output:</p>
<pre><textarea id="statusOutput" rows="10" cols="50"></textarea></pre>
<p>Message output:</p>
<pre><textarea id="messageOutput" rows="10" cols="50"></textarea></pre>
<p>Ping output:</p>
<pre><textarea id="pingOutput" rows="10" cols="50"></textarea></pre>
</body>
</html>
10 changes: 10 additions & 0 deletions chapter17/sockjs/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apply plugin: 'war'

war {
archiveName = 'sockjs.war'
manifest {
attributes("Created-By" : "Iuliana Cosmina",
"Specification-Title": "Pro Spring 5",
"Class-Path" : configurations.compile.collect { it.getName() }.join(' '))
}
}
Loading

0 comments on commit 98bc41a

Please sign in to comment.