Java 在没有端口转发Web服务器的情况下接收Github pubsub事件
我目前编写了以下代码:Java 在没有端口转发Web服务器的情况下接收Github pubsub事件,java,github,websocket,publish-subscribe,Java,Github,Websocket,Publish Subscribe,我目前编写了以下代码: public class Connector { public String auth; public static void main(String[] args) throws IOException, URISyntaxException { Connector c = new Connector(); c.setAuthenticationToken( /* token */ ); JsonObject o = new JsonOb
public class Connector {
public String auth;
public static void main(String[] args) throws IOException, URISyntaxException {
Connector c = new Connector();
c.setAuthenticationToken( /* token */ );
JsonObject o = new JsonObject();
JsonObject hub = new JsonObject();
hub.addProperty("mode", "subscribe");
hub.addProperty("topic", "some_event");
hub.addProperty("callback", "my_public_IP:some_port");
o.add("hub", hub);
String request = o.toString();
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(new JsonParser().parse(c.writeJson("https://api.github.com/hub", REQUEST_METHOD.POST, request))));
}
public void setAuthenticationToken(String token) throws UnsupportedEncodingException {
this.auth = "token " + token;
}
public String writeJson(String strUrl, REQUEST_METHOD method, String data) throws IOException {
URL url = new URL(strUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod(method.name());
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("Content-Type", "application/json; utf-8");
con.setRequestProperty("Authorization", this.auth);
con.setDoOutput(true);
if (data != null) {
try (OutputStream os = con.getOutputStream()) {
byte[] input = data.getBytes("utf-8");
os.write(input, 0, input.length);
} catch (IOException e) {
e.printStackTrace();
}
}
StringBuilder response = new StringBuilder();
try {
int statusCode = con.getResponseCode();
InputStream is = null;
if (statusCode >= 200 && statusCode < 400) {
is = con.getInputStream();
} else {
is = con.getErrorStream();
}
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
} catch (IOException e) {
e.printStackTrace();
}
con.disconnect();
return response.toString();
}
我的目标是打开一个WebSocket,让WebSocket侦听从GitHub发送的事件,而不转发网络上的任何端口,也不使用外部API。
如何指定对未打开的ip:port
的回调?
有没有不使用WebSocket的替代方法?
是否可以创建到https://api.github.com/hub
然后创建指向传出连接端口的webhook,而不转发该特定端口
{
"message": "Invalid hub.callback: \u0027\u0027",
"documentation_url": "https://docs.github.com/rest/reference/repos#pubsubhubbub"
}