我使用Java和JavaScript创建了一个示例项目。 Java用于创建服务器部分,JavaScript用于客户端部分。我使用jetty版本9.0.5来开发javaSE项目。 这是我的服务器部分:
private MultipleOutputs multipleOutputs;
@Override
protected void setup(Context context) throws IOException,
InterruptedException {
multipleOutputs = new MultipleOutputs(context);
}
@Override
protected void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
for (Text value : values) {
multipleOutputs.write(value, new Text(""), key.toString());
}
}
@Override
protected void cleanup(Context context) throws IOException,
InterruptedException {
multipleOutputs.close();
}
这是我的MyWebSocketHandler类
package com.websocet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.websocket.server.WebSocketHandler;
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
public class WebSocketTest {
public static void main(String[] args) throws Exception {
try{
Server server = new Server(8080);
WebSocketHandler wsHandler = new WebSocketHandler() {
@Override
public void configure(WebSocketServletFactory factory) {
factory.register(MyWebSocketHandler.class);
}
};
server.setHandler(wsHandler);
server.start();
server.join();
}catch(Exception e){
System.out.println("Error "+ e);
}
}
}
这是我的客户部分:
package com.websocet;
import java.io.IOException;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
public class MyWebSocketHandler {
@OnWebSocketClose
public void onClose(int statusCode, String reason) {
System.out.println("Close: statusCode=" + statusCode + ", reason=" + reason);
}
@OnWebSocketError
public void onError(Throwable t) {
System.out.println("Error: " + t.getMessage());
}
@OnWebSocketConnect
public void onConnect(Session session) {
System.out.println("Connect: " + session.getRemoteAddress().getAddress());
try {
session.getRemote().sendString("Hello Webbrowser");
} catch (IOException e) {
System.out.println("Error -----" + e);
e.printStackTrace();
}
}
@OnWebSocketMessage
public void onMessage(String message) {
System.out.println("Message: " + message);
}
}
当我运行服务器部件时,它运行良好。
console.log("start index.js");
var ws = new WebSocket("ws://localhost:8080/");
ws.onopen = function() {
console.log("Opened!");
ws.send("Hello Server");
};
ws.onmessage = function (evt) {
console.log("Message: " + evt.data);
};
ws.onclose = function() {
console.log("connection Closed...");
};
ws.onerror = function(err) {
console.log("Error"+ err);
};
但是在我运行客户端部分后,服务器抛出异常。
2015-12-11 12:02:31.081:INFO:oejs.Server:main: jetty-9.0.5.v20130815
2015-12-11 12:02:31.141:INFO:oejs.ServerConnector:main: Started ServerConnector@470e2030{HTTP/1.1}{0.0.0.0:8080}
我下载了 jetty-distribution-9.0.5.v2013081 5并将其添加到 eclipse IDE 中。 我是网络套接字的新手。所以请帮我解决这个问题。我的目标是从我的Web浏览器获取数据并将其传递到我的JavaSE项目中。如果有人知道这样做,请告诉我。
答案 0 :(得分:2)
我找到了问题的答案。我错过了一个注释。必须将其更正为 @WebSocket
@WebSocket
public class MyWebSocketHandler {
......
}