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"/>
</attributes>
</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"/>
</classpath>

View file

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

11
pom.xml
View file

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

View file

@ -1,13 +1,29 @@
package buttondevteam.PlaceMinecraft2;
import java.net.URISyntaxException;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
public class App {
public static void main(String[] args) throws URISyntaxException {
PlaceWebSocket placews = new PlaceWebSocket(
"wss://...");
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;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
public class PlaceWebSocket { //Ported from https://gist.github.com/teaearlgraycold/76877c8f262de24becc081ad96759730
private Gson json;
public JsonObject recv_frame()
{
//frame = super().recv_frame()
return json.fromJson(frame.data.decode('utf-8'));
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.net.URI;
import java.net.URISyntaxException;
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()
{ url = "wss://...";
ws = create_connection(url, class_=PlaceWebSocket);
insert_queue = 0
inserted_count = 0
max_queue_size = 100
save_frame_per = 20000
@Override
public void onOpen(ServerHandshake handshakedata) {
System.out.println("Status: " + handshakedata.getHttpStatusMessage());
}
while(true)
{ try
{
frame = ws.recv_frame();
print(frame);
@Override
public void onMessage(String message) {
JsonObject frame = new JsonParser().parse(message).getAsJsonObject();
String type = frame.get("type").getAsString();
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')
{
c.execute('''INSERT INTO placements VALUES (?, ?, ?, ?, ?)''', [
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
@Override
public void onClose(int code, String reason, boolean remote) {
System.out.println("Code: " + code + " - Reason: " + reason + " - Remote: " + remote);
}
if insert_queue >= max_queue_size:
conn.commit()
insert_queue = 0
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()
@Override
public void onError(Exception ex) {
System.out.println(ex);
}
}