forked from Apress/pro-spring-5
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
100 added websocket working examples
- Loading branch information
Showing
44 changed files
with
837 additions
and
301 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(' ')) | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...m/apress/prospring4/ch17/EchoHandler.java → ...m/apress/prospring5/ch17/EchoHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
chapter17/sockjs-cfg/src/main/java/com/apress/prospring5/ch17/config/WebConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
chapter17/sockjs-cfg/src/main/java/com/apress/prospring5/ch17/config/WebInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]{"/"}; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
chapter17/sockjs-cfg/src/main/java/com/apress/prospring5/ch17/config/WebSocketConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(' ')) | ||
} | ||
} |
Oops, something went wrong.