Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Receive Server-Sent Event Notifications

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Server-Sent Events - One Way Messaging
-------------------------------------------------------------------

A server-sent event is when a web page automatically gets updates from a server.

This was also possible before, but the web page would have to ask if any updates were available. With server-sent events, the updates come automatically.

Examples: Facebook/Twitter updates, stock price updates, news feeds, sport results, etc.

1) Create a new EventSource object, and specify the URL of the page sending the updates (in this example "ServerSendEvent" Servelet page)
2) Each time an update is received, the onmessage event occurs
3) When an onmessage event occurs, put the received data into the element with id="result"

Create index.html
------------------------
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Getting server updates</h1>
<div id="result"></div>

<script>
if(typeof(EventSource)!=="undefined")
{
var source=new EventSource("ServerSendEvent");
source.onmessage=function(event)
{
document.getElementById("result").innerHTML+=event .data + "<br />";
};
}
else
{
document.getElementById("result").innerHTML="Sorry , your browser does not support server-sent events...";
}
</script>

</body>
</html>

-------------------------------------------------------------------------

Create New servlet page in the name of 'ServerSendEvent'

-------------------------------------------------------------------------
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @author Senthil Kumar
*/
public class ServerSendEvent extends HttpServlet {


@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("Begin for session: " + request.getSession(true).getId() + " " + response.getWriter());
response.setHeader("pragma", "no-cache,no-store");
response.setHeader("cache-control", "no-cache,no-store,max-age=0,max-stale=0");

response.setContentType("text/event-stream");

PrintWriter out = response.getWriter();

int messagesSent = 0;
while (true) {
out.print("data: {" + messagesSent++ + "}\n\n");
out.flush();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
System.out.println("Sent " + messagesSent);
}

}


@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

}


@Override
public String getServletInfo() {
return "Short description";
}

}
------------------------------------------------------------------------------

 
Sheriff
Posts: 67637
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what's your question?
 
This cake looks terrible, but it tastes great! Now take a bite out of this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic