This repository has been archived by the owner on Mar 20, 2021. It is now read-only.
forked from NicolasBailo/lab1-twitter-stream
-
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.
- Loading branch information
Showing
8 changed files
with
240 additions
and
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,47 @@ | ||
buildscript { | ||
ext { | ||
springBootVersion = '1.5.10.RELEASE' | ||
} | ||
repositories { | ||
mavenCentral() | ||
} | ||
dependencies { | ||
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") | ||
} | ||
} | ||
|
||
apply plugin: 'java' | ||
apply plugin: 'org.springframework.boot' | ||
apply plugin: 'idea' | ||
apply plugin: 'eclipse-wtp' | ||
|
||
group = 'es.unizar.tmdad' | ||
version = '2018' | ||
sourceCompatibility = 1.8 | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
ext { | ||
boostrapVersion = '4.0.0' | ||
fontawesomeVersion = '5.0.6' | ||
mustacheVersion = '2.3.0' | ||
} | ||
|
||
dependencies { | ||
compile('org.springframework.boot:spring-boot-starter-social-twitter') | ||
compile('org.springframework.boot:spring-boot-starter-thymeleaf') | ||
compile('org.springframework.boot:spring-boot-starter-web') | ||
compile('org.springframework.boot:spring-boot-devtools') | ||
compile("org.webjars:bootstrap:${boostrapVersion}") | ||
compile("org.webjars:font-awesome:${fontawesomeVersion}") | ||
compile("org.webjars.bower:mustache:${mustacheVersion}") | ||
testCompile('org.springframework.boot:spring-boot-starter-test') | ||
buildscript { | ||
ext { | ||
springBootVersion = '1.5.10.RELEASE' | ||
} | ||
repositories { | ||
mavenCentral() | ||
} | ||
dependencies { | ||
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") | ||
} | ||
} | ||
|
||
apply plugin: 'java' | ||
apply plugin: 'org.springframework.boot' | ||
apply plugin: 'idea' | ||
apply plugin: 'eclipse-wtp' | ||
|
||
group = 'es.unizar.tmdad' | ||
version = '2018' | ||
sourceCompatibility = 1.8 | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
ext { | ||
boostrapVersion = '4.0.0' | ||
fontawesomeVersion = '5.0.6' | ||
//mustacheVersion = '2.3.0' | ||
} | ||
|
||
dependencies { | ||
compile('org.springframework.boot:spring-boot-starter-social-twitter') | ||
compile('org.springframework.boot:spring-boot-starter-thymeleaf') | ||
compile('org.springframework.boot:spring-boot-starter-web') | ||
compile('org.springframework.boot:spring-boot-devtools') | ||
compile("org.webjars:bootstrap:${boostrapVersion}") | ||
compile("org.webjars:font-awesome:${fontawesomeVersion}") | ||
//compile("org.webjars.bower:mustache:${mustacheVersion}") | ||
|
||
compile("org.springframework.boot:spring-boot-starter-websocket") | ||
compile("org.webjars:stomp-websocket:2.3.3") | ||
compile("org.webjars:sockjs-client:1.0.2") | ||
|
||
|
||
testCompile('org.springframework.boot:spring-boot-starter-test') | ||
} |
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
64 changes: 64 additions & 0 deletions
64
src/main/java/es/unizar/tmdad/lab0/service/SimpleStreamListener.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,64 @@ | ||
/* | ||
* AUTOR: Abel Naya Forcano | ||
* NIA: 544125 | ||
* FICHERO: SimpleStreamListener.java | ||
* TIEMPO: - | ||
* DESCRIPCI’ON: | ||
*/ | ||
|
||
package es.unizar.tmdad.lab0.service; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.messaging.MessageHeaders; | ||
import org.springframework.messaging.handler.annotation.DestinationVariable; | ||
import org.springframework.messaging.handler.annotation.SendTo; | ||
import org.springframework.messaging.simp.SimpMessageSendingOperations; | ||
import org.springframework.social.twitter.api.StreamDeleteEvent; | ||
import org.springframework.social.twitter.api.StreamListener; | ||
import org.springframework.social.twitter.api.StreamWarningEvent; | ||
import org.springframework.social.twitter.api.Tweet; | ||
import org.springframework.util.MimeTypeUtils; | ||
|
||
/** | ||
* Clase SimpleStreamListener | ||
*/ | ||
public class SimpleStreamListener implements StreamListener{ | ||
|
||
|
||
private final SimpMessageSendingOperations smso; | ||
private final String query; | ||
|
||
SimpleStreamListener(String query, SimpMessageSendingOperations smso) { | ||
this.query = query; | ||
this.smso = smso; | ||
} | ||
|
||
@Override | ||
public void onTweet(Tweet tweet) { | ||
System.out.println("Received tweet, sending ("+query+")"); | ||
|
||
Map<String, Object> map = new HashMap<>(); | ||
map.put(MessageHeaders.CONTENT_TYPE,MimeTypeUtils.APPLICATION_JSON); | ||
smso.convertAndSend("/topic/search/"+query, tweet, map); | ||
|
||
} | ||
|
||
|
||
@Override | ||
public void onDelete(StreamDeleteEvent deleteEvent) { | ||
|
||
} | ||
|
||
@Override | ||
public void onLimit(int numberOfLimitedTweets) { | ||
System.out.println("SimpleStreamListener#onLimit"); | ||
} | ||
|
||
@Override | ||
public void onWarning(StreamWarningEvent warningEvent) { | ||
System.out.println("SimpleStreamListener#onWarning"); | ||
} | ||
|
||
} |
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
24 changes: 24 additions & 0 deletions
24
src/main/java/es/unizar/tmdad/lab0/service/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,24 @@ | ||
package es.unizar.tmdad.lab0.service; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.messaging.simp.config.MessageBrokerRegistry; | ||
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer; | ||
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; | ||
import org.springframework.web.socket.config.annotation.StompEndpointRegistry; | ||
|
||
@Configuration | ||
@EnableWebSocketMessageBroker | ||
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { | ||
|
||
@Override | ||
public void registerStompEndpoints(StompEndpointRegistry registry) { | ||
registry.addEndpoint("/twitter").withSockJS(); | ||
} | ||
|
||
@Override | ||
public void configureMessageBroker(MessageBrokerRegistry registry) { | ||
registry.enableSimpleBroker("/topic"); | ||
registry.setApplicationDestinationPrefixes("/app"); | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -1,37 +1,51 @@ | ||
var stompClient = null; | ||
var mustacheTemplate = "-unloaded-"; | ||
var subscription = null; | ||
|
||
$(document).ready(function() { | ||
|
||
$("#search").submit(searchOnClick); | ||
|
||
mustacheTemplate = "-unloaded-"; | ||
$("#search").submit(streamOnClick); | ||
|
||
$.get('template', function(template) { | ||
Mustache.parse(template); | ||
mustacheTemplate = template; | ||
}); | ||
|
||
|
||
stompClient = Stomp.over(new SockJS("/twitter"));//endpoint | ||
stompClient.connect({}, function(frame) { | ||
stompClient.debug = null; | ||
console.log("Connected"); | ||
}); | ||
}); | ||
|
||
function searchOnClick(event) { | ||
|
||
|
||
function streamOnClick(event){ | ||
event.preventDefault(); | ||
|
||
$("#resultsBlock").empty(); | ||
$("#loader").show(); | ||
|
||
var target = $(this).attr('action'); | ||
var query = $("#q").val(); | ||
$.get(target, { q: query } ) | ||
.done( onPostQuery ) | ||
.fail( onFailQuery ); | ||
} | ||
|
||
function onPostQuery(data){ | ||
|
||
var rendered = Mustache.render(mustacheTemplate, data); | ||
|
||
$("#loader").hide(); | ||
$("#resultsBlock").append(rendered); | ||
if(subscription !== null){ | ||
stompClient.send("/app/unregister",{},subscription[1]); | ||
subscription[0].unsubscribe(); | ||
} | ||
|
||
stompClient.send("/app/register",{},query); | ||
subscription = [stompClient.subscribe("/topic/search/"+query, onTweetReceived),query]; | ||
console.log("subscribed to >>"+query); | ||
|
||
} | ||
|
||
function onFailQuery(){ | ||
|
||
function onTweetReceived(tweet){ | ||
console.log("received tweet"); | ||
var rendered = Mustache.render(mustacheTemplate, JSON.parse(tweet.body)); | ||
|
||
$("#loader").hide(); | ||
$("#resultsBlock").append("-error-"); | ||
$("#resultsBlock").append(rendered); | ||
$("#resultsBlock").scrollTop(); | ||
} |
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