我有一个websocket聊天应用程序,我想在websocket上显示图像,id =“received_messages”我不知道如何在 网络套接字onMessage(evnt)所以有人发送消息应该是他的图像和发送日期我要求帮助PLZ
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet"
type="text/css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="websocket.js" type="text/javascript"></script>
</h:head>
<h:body>
<div class="container">
<h1>Java API for WebSocket (JSR-356)</h1>
<div>
<span id="status" class="label label-important">Not Connected</span>
</div>
<br />
<c:if
test="${candidateController.connectedCandidate.idUserInformation!=0}">
<h:inputText type="text" id="name"
value="#{candidateController.connectedCandidate.name}" />
</c:if>
<c:if test="${companyController.idCompany!=0}">
<h:inputText type="text" id="name"
value="#{companyController.companyName(companyController.idCompany)}" />
</c:if>
<h:outputText
value="#{candidateController.connectedCandidate.idUserInformation}"></h:outputText>
<label style="display: inline-block">Message: </label> <input
type="text" id="message" />
<button id="send" class="btn btn-primary" onclick="sendMessage()">Send</button>
<table id="received_messages" class="table table-striped">
<tr>
<th>#</th>
<th></th>
<th>Message</th>
<th>Sender</th>
<th><c:if
test="${candidateController.connectedCandidate.idUserInformation!=0}">
<p>
<h:outputText
value="${candidateController.connectedCandidate.idUserInformation}" />
</p>
</c:if> <c:if test="${companyController.idCompany!=0}">
<p>
<h:outputText value="${companyController.idCompany}" />
</p>
</c:if>
<img id="image"></img></th>
</tr>
</table>
</div>
<script>
var URL = "ws://153.126.185.185:8282/websocket/";
var websocket;
var name;
var currentdate = new Date();
var datetime = "On: " + currentdate.getDate() + "/"
+ (currentdate.getMonth()+1) + "/"
+ currentdate.getFullYear() + " "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
$(document).ready(function() {
connect();
});
function connect() {
websocket = new WebSocket(URL);
websocket.onopen = function(evnt) {
onOpen(evnt)
};
websocket.onmessage = function(evnt) {
onMessage(evnt)
};
websocket.onerror = function(evnt) {
onError(evnt)
};
}
function sendMessage() {
websocket.send($("#name").val()+' Says: '+$("#message").val()+' '+datetime);
websocket.toString();
}
function onOpen() {
websocket.send(document.getElementById("name").value + " has joined");
updateStatus("connected");
}
function onMessage(evnt) {
if (typeof evnt.data == "string") {
$("#received_messages")
.append(
$('<tr/>')
.append(
$('<td/>')
.text(
"46511103"))
.append(
$('<td/>')
.text(
evnt.data
.substring(
0,
evnt.data
.indexOf(":"))))
.append(
$('<td/>')
.text(
evnt.data
.substring(evnt.data
.indexOf(":") + 1)))
);
}
}
function onError(evnt) {
alert('ERROR: ' + evnt.data);
}
function updateStatus(status) {
if (status == "connected") {
$("#status").removeClass(function(index, css) {
return (css.match(/\blabel-\S+/g) || []).join(' ')
});
$("#status").text(status).addClass("label-success");
}
}
</script>
package com.hrdatabank.chat;
import java.io.IOException;
import javax.websocket.ClientEndpoint;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
@ClientEndpoint
public class MyClient {
@OnOpen
public void onOpen(Session session) {
System.out.println("Connected to endpoint: " + session.getBasicRemote());
try {
session.getBasicRemote().sendText("Hello");
} catch (IOException ex) {
}
}
@OnMessage
public void onMessage(String message) {
System.out.println(message);
}
@OnError
public void onError(Throwable t) {
t.printStackTrace();
}
}
package com.hrdatabank.chat; import java.io.IOException; import java.util.LinkedList; import javax.websocket.EncodeException; import javax.websocket.OnClose; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.PathParam; import javax.websocket.server.ServerEndpoint; @ServerEndpoint(value="/websocket/{client-id}") public class MyServerEndpoint { private static final LinkedList<Session> clients = new LinkedList<Session>(); @OnOpen public void onOpen(Session session) { clients.add(session); } @OnMessage public void onMessage(String message,@PathParam("client-id") String clientId) { for (Session client : clients) { try { client.getBasicRemote().sendObject(clientId+": "+message); } catch (IOException e) { e.printStackTrace(); } catch (EncodeException e) { e.printStackTrace(); } } } @OnClose public void onClose(Session peer) { clients.remove(peer); } }