public void dungeonEnded() { dungeonEndedClient(); Socket socket = field.getClientCharacter().getRoom().getSocket(); if (socket != null && socket.connected()) { socket.emit("dungeonEnded"); } }
@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_cards_now, container, false); initViewsAndAdapters(); initListView(); FrameLayout mFrameLayout = (FrameLayout) view.findViewById(R.id.now_cards_list); mFrameLayout.addView(getExpandableListView()); mSocket.on( EVENT_INIT, args -> { UI.execute( () -> { JSONObject jsonObject = (JSONObject) args[0]; try { JSONArray jsonArray = jsonObject.getJSONArray(INIT_BEACONS_KEY); for (int i = 0; i < jsonArray.length(); i++) { JSONObject beacon = (JSONObject) jsonArray.get(i); String identifier = beacon.getString(ID_KEY); int floor = beacon.getInt(FLOOR_KEY); BeaconLocationManager bm = HereAndNowApplication.getBeaconLocationManager(); bm.addBeacon(identifier, floor, getContext()); } // Start the manager. HereAndNowApplication.getBeaconLocationManager().resume(); // Initialize cards. JSONArray cardsArray = jsonObject.getJSONArray(INIT_MESSAGES_KEY); setupInitialView(cardsArray); } catch (JSONException e) { e.printStackTrace(); } }); }); mSocket.emit(EVENT_INIT); // Requests latest messages initTopicsAndCards(createPreBuiltTopics(), getSourceTopicModel(), getTopicListAdapter()); filterModel(); return view; }
// gets user credentials from SharedPreferences private DataStore(Context context) { SharedPreferences settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE); username = settings.getString(USERNAME_KEY, ""); password = settings.getString(PASSWORD_KEY, ""); id = settings.getString(ID_KEY, ""); mSocket.connect(); }
public void connect() { try { socket = IO.socket(serverLocation); socket.connect(); configureEvents(); } catch (URISyntaxException e) { log("Error connecting to socket", e); } }
public void sendUpdate(float delta) { timer += delta; if (timer >= UPDATE_TIME && screen.getPlayer() != null && screen.getPlayer().hasMoved) { JSONObject data = new JSONObject(); try { data.put("x", screen.getPlayer().getX()); data.put("y", screen.getPlayer().getY()); data.put("l", screen.getPlayer().getLocationIdentifier()); socket.emit(EVENT_PLAYER_MOVED, data); } catch (JSONException e) { Gdx.app.log("SocketIO", "Error sending update"); } } }
public void connect(String url, final String userId) throws URISyntaxException { Log.w(TAG, "f**k the log"); if (TextUtils.isEmpty(url)) { FSLog.w(TAG, "url is empty"); return; } IO.Options options = new IO.Options(); options.forceNew = true; socket = IO.socket(IMConfig.IP + ":3005", options); socket.on( Socket.EVENT_CONNECT, new Emitter.Listener() { @Override public void call(Object... args) { FSLog.w(TAG, "bull shit"); socket.emit( "getUserId", userId, new Ack() { @Override public void call(Object... args) {} }); if (loginCallback != null) { loginCallback.onSuccess(); loginCallback = null; } } }); socket.on( Socket.EVENT_DISCONNECT, new Emitter.Listener() { @Override public void call(Object... args) { FSLog.w(TAG, "disconnect"); } }); socket.on( Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() { @Override public void call(Object... args) { FSLog.w(TAG, "connect error"); if (loginCallback != null) { loginCallback.onFailed(); loginCallback = null; } } }); socket.on( Socket.EVENT_ERROR, new Emitter.Listener() { @Override public void call(Object... args) { FSLog.w(TAG, "EVENT_ERROR"); } }); socket.on( Socket.EVENT_CONNECT_TIMEOUT, new Emitter.Listener() { @Override public void call(Object... args) { FSLog.w(TAG, "time out"); } }); socket.on( "message", new Emitter.Listener() { @Override public void call(Object... args) { FSLog.w(TAG, args[0].toString()); /* { contentStr = helloWorld; fromUserId = 6; msgContentType = 1; msgId = "768D2D05-046C-4E3C-B39A-04FC5A0245DE"; sendDate = "1474811811.949114"; serverReceiveDate = 1474811812153; state = 1; toUserId = 7; } */ } }); socket.connect(); FSLog.w("zxc", "connect"); }
private void emitEvent(Event event) { if (socket != null && socket.connected()) { socket.emit("newEvent", event.toJSON()); } }
private void emit(final String event, final Object... args) { if (mSocket.connected()) mSocket.emit(event, args); }
private void initSocket() { try { IO.Options options = new IO.Options(); // options.reconnectionAttempts = 1; // options.forceNew = true; options.reconnectionDelay = 1000; options.reconnectionDelayMax = 2000; options.timeout = 2000; mSocket = IO.socket(Constants.CHAT_SERVER_URL, options); mSocket.on(SocketEvent.CONNECT, onConnect); mSocket.on(SocketEvent.DISCONNECT, onDisconnect); mSocket.on(SocketEvent.CONNECT_ERROR, onConnectError); mSocket.on(SocketEvent.CONNECT_TIMEOUT, onConnectError); mSocket.on(SocketEvent.LOGIN, onLogin); mSocket.on(SocketEvent.USER_JOIN, onUserJoin); mSocket.on(SocketEvent.USER_LEFT, onUserLeft); mSocket.on(SocketEvent.TYPING, onTyping); mSocket.on(SocketEvent.STOP_TYPING, onStopTyping); mSocket.on(SocketEvent.LIST_CONTACT, onListContact); mSocket.on(SocketEvent.UPDATE_CONTACT, onUpdateContact); mSocket.on(SocketEvent.NEW_MESSAGE, onNewMessage); mSocket.on(SocketEvent.DELIVERY_STATUS, onDeliveryStatus); mSocket.connect(); } catch (URISyntaxException e) { Log.e(Constants.TAG_CHAT, e.getMessage(), e); } }
public boolean isConnected() { return mSocket.connected(); }
private void configureEvents() { socket .on( Socket.EVENT_CONNECT, new Emitter.Listener() { @Override public void call(Object... args) { log("Connected"); } }) .on( EVENT_SOCKET_ID, new Emitter.Listener() { // called when connecting @Override public void call(Object... args) { JSONObject data = (JSONObject) args[0]; try { id = data.getString("id"); log(String.format("You connected with id %s", id)); } catch (JSONException e) { log("Error getting id", e); } } }) .on( EVENT_PLAYER_CONNECTED, new Emitter.Listener() { // Called @Override public void call(Object... args) { JSONObject object = (JSONObject) args[0]; try { log("Another player connected"); object = object.getJSONObject("p"); screen.playerJoins( object.getString("id"), object.getString("locationId"), ((Double) object.getDouble("x")).floatValue(), ((Double) object.getDouble("y")).floatValue()); } catch (JSONException e) { log("Error getting id", e); } } }) .on( EVENT_PLAYER_DISCONNECTED, new Emitter.Listener() { @Override public void call(Object... args) { JSONObject data = (JSONObject) args[0]; try { String id = data.getString("id"); screen.getOnlinePlayers().remove(id); log(String.format("online player id %s", id)); } catch (JSONException e) { log("Error getting id", e); } } }) .on( EVENT_ONLINE_PLAYERS, new Emitter.Listener() { @Override public void call(Object... args) { JSONArray objects = (JSONArray) args[0]; try { for (int i = 0; i < objects.length(); i++) { screen.playerJoins( objects.getJSONObject(i).getString("id"), objects.getJSONObject(i).getString("locationId"), ((Double) objects.getJSONObject(i).getDouble("x")).floatValue(), ((Double) objects.getJSONObject(i).getDouble("y")).floatValue()); } } catch (JSONException e) { log("Error getting id", e); } } }) .on( EVENT_PLAYER_MOVED, new Emitter.Listener() { @Override public void call(Object... args) { JSONObject data = (JSONObject) args[0]; try { String playerId = data.getString("id"); Double x = data.getDouble("x"); Double y = data.getDouble("y"); String locationId = data.getString("l"); if (screen.getOnlinePlayers().get(playerId) != null) { screen .getOnlinePlayers() .get(playerId) .setPosition(x.floatValue(), y.floatValue()); } } catch (JSONException e) { e.printStackTrace(); } } }); }
public void disconnect() { socket.disconnect(); }