Skip to content

Commit

Permalink
Small enhancements in WebSocket example, refs #390
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Sep 12, 2015
1 parent 0f87892 commit da713fe
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@

.wrapper {
margin-left: auto;
margin-right: auto;
width: 1084px;
text-align: center;
}

.shadow {
border: 1px solid #f00;
transition: all .5s ease-in;
}

img {
margin-left: 8px;
margin-bottom: 8px;
width: 348px;
height: 198px;
box-shadow: 0 0 16px #444;
border: 1px solid #fff;
transition: all 1s ease-in;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ $(document).ready(function() {
$webcams.append($img)
}
} else if (type === 'image') {
$("img[name='" + data.webcam + "']")
var $img = $("img[name='" + data.webcam + "']")
.attr("src", "data:image/jpeg;base64," + data.image)
.addClass('shadow')
.trigger("change");
setTimeout(function() {
$img
.removeClass('shadow')
.trigger("change");
}, 1000);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamEvent;
import com.github.sarxos.webcam.WebcamListener;
Expand All @@ -12,6 +15,8 @@

public class WebcamCache implements WebcamUpdater.DelayCalculator, WebcamListener {

private static final Logger LOG = LoggerFactory.getLogger(WebcamCache.class);

/**
* How often images are updated on Dasding server.
*/
Expand Down Expand Up @@ -46,7 +51,13 @@ public long calculateDelay(long snapshotDuration, double deviceFps) {
}

public static BufferedImage getImage(String name) {
return CACHE.webcams.get(name).getImage();
Webcam webcam = CACHE.webcams.get(name);
try {
return webcam.getImage();
} catch (Exception e) {
LOG.error("Exception when getting image from webcam", e);
}
return null;
}

public static List<String> getWebcamNames() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@ public void newImage(Webcam webcam, BufferedImage image) {
}

private void send(String message) {
try {
session.getRemote().sendString(message);
} catch (IOException e) {
LOG.error("Exception when sending string", e);
if (session.isOpen()) {
try {
session.getRemote().sendStringByFuture(message);
} catch (Exception e) {
LOG.error("Exception when sending string", e);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.websocket.server.WebSocketHandler;
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.ds.ipcam.IpCamDriver;
import com.github.sarxos.webcam.ds.ipcam.IpCamStorage;


/**
* This example demonstrates how webcam capture IP camera driver can be used
* with conjunction with websockets to feed data to the web application
* frontend.
* This example demonstrates how webcam capture IP camera driver can be used with conjunction with
* websockets to feed data to the web application frontend.
*
* @author Bartosz Firyn (sarxos)
*/
Expand All @@ -20,10 +21,12 @@ public class WebcamWebSocketsExample {
Webcam.setDriver(new IpCamDriver(new IpCamStorage("src/main/resources/cameras.xml")));
}

private static final Logger LOG = LoggerFactory.getLogger(WebcamWebSocketsExample.class);

public static void main(String[] args) throws Exception {

for (String name : WebcamCache.getWebcamNames()) {
System.out.println("Will read webcam " + name);
LOG.info("Will read webcam {}", name);
}

Server server = new Server(8123);
Expand Down

0 comments on commit da713fe

Please sign in to comment.