I have a WebSocket server implemented in Java. When a client connects I want to send an image over this connection for the client to use in a canvas element. I have come up with the following server code:
public void onOpen(Connection connection) {
try {
BufferedImage image = ImageIO.read(new File("image.jpg"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);
byte[] byteArray = baos.toByteArray();
connection.sendMessage(byteArray, 0, byteArray.length);
} catch (Exception e ){
System.out.println("Error: "+e.getMessage());
}
}
The client-side Javascript looks like this:
onmessage : function(m) {
if (m.data) {
if (m.data instanceof Blob) {