Last active
November 2, 2015 16:16
-
-
Save caub/33aa31b380af6b581cb3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ws; | |
import java.lang.reflect.Type; | |
import java.net.CookieManager; | |
import java.net.CookieStore; | |
import java.net.HttpCookie; | |
import java.net.URI; | |
import java.util.LinkedHashMap; | |
import java.util.concurrent.Future; | |
import org.eclipse.jetty.client.HttpClient; | |
import org.eclipse.jetty.client.api.ContentResponse; | |
import org.eclipse.jetty.util.Fields; | |
import org.eclipse.jetty.util.Fields.Field; | |
import org.eclipse.jetty.util.ssl.SslContextFactory; | |
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; | |
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest; | |
import org.eclipse.jetty.websocket.client.WebSocketClient; | |
import org.jsoup.Jsoup; | |
import com.google.gson.Gson; | |
import com.google.gson.reflect.TypeToken; | |
@WebSocket | |
public class SEChat { | |
String email = System.getProperty("se.email"); | |
String password = System.getProperty("se.password"); | |
public static void main(String[] args) throws Exception { | |
SEChat m = new SEChat(); | |
m.run(); | |
} | |
Gson gson = new Gson(); | |
Type mapType = new TypeToken<LinkedHashMap<String, Object>>() {}.getType(); | |
public void run() throws Exception { | |
SslContextFactory sslContextFactory = new SslContextFactory(); | |
sslContextFactory.setTrustAll(true); | |
HttpClient httpClient = new HttpClient(sslContextFactory); | |
CookieStore cookieStore = new CookieManager().getCookieStore(); | |
httpClient.setCookieStore(cookieStore); | |
httpClient.start(); | |
ContentResponse response = httpClient.GET("https://stackoverflow.com/users/login"); | |
String fkey = Jsoup.parse(response.getContentAsString()) | |
.select("input[name=fkey]").attr("value"); | |
System.out.printf("login: %s, fkey: %s%n", response.getStatus(), fkey); | |
response = httpClient.FORM("https://stackoverflow.com/users/login", | |
fields("email", email, "password", password, "fkey", fkey)); | |
System.out.printf("login submit: %s%n", response.getStatus()); | |
response = httpClient.GET("http://chat.stackoverflow.com/rooms/1"); | |
fkey = Jsoup.parse(response.getContentAsString()) | |
.select("input[name=fkey]").attr("value"); | |
System.out.printf("chat: %s, fkey: %s%n", response.getStatus(), fkey); | |
response = httpClient.FORM("http://chat.stackoverflow.com/ws-auth", | |
fields("roomid", "1", "fkey", fkey)); | |
LinkedHashMap<String, Object> o = gson.fromJson(response.getContentAsString(), mapType); | |
String url = (String) o.get("url"); | |
System.out.printf("ws-auth: %s, url: %s%n", response.getStatus(), url); | |
response = httpClient.FORM("http://chat.stackoverflow.com/chats/1/events", fields("fkey", fkey)); | |
o = gson.fromJson(response.getContentAsString(), mapType); | |
Double time = (Double) o.get("time"); | |
System.out.printf("chat-events: %s, time: %s%n", response.getStatus(), time); | |
//System.setProperty("org.eclipse.jetty.LEVEL", "DEBUG"); | |
WebSocketClient wsClient = new WebSocketClient(sslContextFactory); | |
wsClient.setCookieStore(cookieStore); | |
for(HttpCookie c : cookieStore.getCookies()) | |
System.out.printf(" - %s - %s%n",c,c.getDomain()+c.getPath()); | |
try { | |
wsClient.start(); | |
ClientUpgradeRequest request = new ClientUpgradeRequest(); | |
//request.setCookiesFrom(cookieStore); | |
request.setHeader("Host","chat.sockets.stackexchange.com"); | |
request.setHeader("Origin","http://chat.stackoverflow.com"); | |
Future<Session> future = wsClient.connect(getClass().newInstance(), URI.create(url+"?l="+time),request); | |
Session session = future.get(); | |
session.getRemote().sendString("hello"); | |
} catch (Throwable t) { | |
t.printStackTrace(System.err); | |
} | |
} | |
@OnWebSocketConnect | |
public void onConnect(Session sess){ | |
System.out.println("onConnect"); | |
} | |
@OnWebSocketClose | |
public void onClose(int statusCode, String reason){ | |
System.out.println("onClose "+ reason); | |
} | |
@OnWebSocketError | |
public void onError(Throwable cause){ | |
System.out.println(cause); | |
} | |
@OnWebSocketMessage | |
public void onMessage(String msg){ | |
System.out.println("onMessage "+ msg); | |
} | |
public Fields fields(String... a){ | |
Fields fields = new Fields(); | |
for(int i=0; i<a.length; i+=2) | |
fields.put(new Field(a[i], a[i+1])); | |
return fields; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Alternative approach.