Finished porting, successfully got data

This commit is contained in:
Norbi Peti 2017-04-01 11:27:34 +02:00
parent 3df935b7bc
commit c31399155a
5 changed files with 81 additions and 63 deletions

View file

@ -22,5 +22,10 @@
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/> <classpathentry kind="output" path="target/classes"/>
</classpath> </classpath>

View file

@ -2,3 +2,4 @@ eclipse.preferences.version=1
encoding//src/main/java=UTF-8 encoding//src/main/java=UTF-8
encoding//src/test/java=UTF-8 encoding//src/test/java=UTF-8
encoding/<project>=UTF-8 encoding/<project>=UTF-8
encoding/src=UTF-8

11
pom.xml
View file

@ -55,8 +55,12 @@
<id>jitpack.io</id> <id>jitpack.io</id>
<url>https://jitpack.io/</url> <url>https://jitpack.io/</url>
</repository> </repository>
<repository>
<id>clojars.org</id>
<url>http://clojars.org/repo</url>
</repository>
</repositories> </repositories>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
@ -70,5 +74,10 @@
<version>1.11.2-R0.1-SNAPSHOT</version> <version>1.11.2-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>com.github.TooTallNate</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>Java-WebSocket-1.3.1</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

View file

@ -1,13 +1,29 @@
package buttondevteam.PlaceMinecraft2; package buttondevteam.PlaceMinecraft2;
import java.net.URISyntaxException;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
/** /**
* Hello world! * Hello world!
* *
*/ */
public class App public class App {
{ public static void main(String[] args) throws URISyntaxException {
public static void main( String[] args ) PlaceWebSocket placews = new PlaceWebSocket(
{ "wss://...");
System.out.println( "Hello World!" ); SSLContext sslContext = null;
} try {
sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, null, null); // will use java's default key and trust store which is sufficient unless you deal with self-signed certificates
SSLSocketFactory factory = sslContext.getSocketFactory();// (SSLSocketFactory) SSLSocketFactory.getDefault();
placews.setSocket(factory.createSocket());
if (!placews.connectBlocking())
System.out.println("Failed to connect.");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Finished");
}
} }

View file

@ -1,65 +1,52 @@
package buttondevteam.PlaceMinecraft2; package buttondevteam.PlaceMinecraft2;
import com.google.gson.Gson; import org.java_websocket.client.WebSocketClient;
import com.google.gson.JsonObject; import org.java_websocket.handshake.ServerHandshake;
public class PlaceWebSocket { //Ported from https://gist.github.com/teaearlgraycold/76877c8f262de24becc081ad96759730 import com.google.gson.JsonObject;
private Gson json; import com.google.gson.JsonParser;
public JsonObject recv_frame()
{ import java.net.URI;
//frame = super().recv_frame() import java.net.URISyntaxException;
return json.fromJson(frame.data.decode('utf-8'));
public class PlaceWebSocket extends WebSocketClient { // Ported from https://gist.github.com/teaearlgraycold/76877c8f262de24becc081ad96759730
public PlaceWebSocket(String wslink) throws URISyntaxException {
super(new URI(wslink));
/*
* insert_queue = 0 inserted_count = 0 max_queue_size = 100 save_frame_per = 20000
*/
} }
public void main() @Override
{ url = "wss://..."; public void onOpen(ServerHandshake handshakedata) {
ws = create_connection(url, class_=PlaceWebSocket); System.out.println("Status: " + handshakedata.getHttpStatusMessage());
insert_queue = 0 }
inserted_count = 0
max_queue_size = 100
save_frame_per = 20000
while(true) @Override
{ try public void onMessage(String message) {
{ JsonObject frame = new JsonParser().parse(message).getAsJsonObject();
frame = ws.recv_frame(); String type = frame.get("type").getAsString();
print(frame); JsonObject payload = frame.get("payload").getAsJsonObject();
System.out.println("Type: " + type);
if (type.equals("place")) {
int x = payload.get("x").getAsInt();
int y = payload.get("y").getAsInt();
String color = payload.get("color").getAsString();
String author = payload.get("author").getAsString();
System.out.println("X: " + x + " - Y: " + y + " - Color: " + color + " - Author: " + author);
} else if (type.equals("activity")) {
String count = payload.get("count").getAsString();
System.out.println("Count: " + count);
}
}
if(frame['type'] == 'place') @Override
{ public void onClose(int code, String reason, boolean remote) {
c.execute('''INSERT INTO placements VALUES (?, ?, ?, ?, ?)''', [ System.out.println("Code: " + code + " - Reason: " + reason + " - Remote: " + remote);
int(time.time()), }
frame['payload']['x'],
frame['payload']['y'],
frame['payload']['color'],
frame['payload']['author']
])
insert_queue += 1
inserted_count += 1
}
else if(frame['type'] == 'activity')
{
c.execute('''INSERT INTO activity VALUES (?, ?)''', [
int(time.time()),
frame['payload']['count']
])
insert_queue += 1
inserted_count += 1
if insert_queue >= max_queue_size: @Override
conn.commit() public void onError(Exception ex) {
insert_queue = 0 System.out.println(ex);
if inserted_count % save_frame_per == 0: }
save_bitmap(c, conn)
except KeyboardInterrupt:
print('Exiting safely...')
conn.commit()
conn.close()
sys.exit()
except Exception as e:
print('Error occured: {}'.format(str(e)))
if __name__ == '__main__':
main()
} }