У меня проблема ... Я пишу уведомление android приложение, реализующее socket.io, пока что все отлично работает, кроме номеров. Я успешно присоединился к комнате, но я ничего не могу отправить в эту комнату, поэтому я могу получить console.log (), по крайней мере.
Мой индекс. js (socket.io)
const express = require('express');
const app = module.exports = express();
const mongoose = require('mongoose');
const User = require('../../models/User/User.js');
const Alerts = require('../../models/Alerts/Alerts.js');
const http = require("http");
const socketIo = require("socket.io");
const server = http.createServer(app);
const io = socketIo(server); // < Interesting!
const notifications = io.of('/notifications')
const changeStream = Alerts.watch();
server.listen(3004, () => console.log(`Listening on port 3004`));
function returnTimes(socket) {
Alerts.find({
deleted: false,
}, function(err, data) {
if (err) {
console.log("/pazinojumi/lietotnei error", err.message, new Date());
res.status(200).send({
error: 'Neizdevās atgriezt paziņojumu sarakstu',
});
return;
}
let laiki = [];
for (let i = 0; i < data.length; i++) {
const { time, reactionTime } = data[i];
const response = {
time,
reactionTime,
};
laiki.push(response);
console.log("userId", response.time);
}
// console.log("1", laiki[1].time);
notifications.emit('log', {laiki});
});
// console.log("1");
}
notifications.on('connection', function(socket) {
socket._connectTimer = setTimeout(function() {
socket.close();
}, 10000);
changeStream.on('change', (change) => {
console.log("Parsuta jaunus datus");
const response = returnTimes(socket);
});
const response = returnTimes(socket);
socket.on('join', function() {
console.log("Someone joined the room."); // this won't show.
});
socket.join("DTG", () => {
console.log(socket.rooms); // this works, i get a roomname to which i'm joined
notifications.to("DTG").emit('join'); // suppose this aint working.
});
// socket.join('DTG')
// notifications.to('DTG').emit('join', data);
console.log(socket.handshake.query.userId, "Connected!" + socket.id, "response", response, new Date());
clearTimeout(socket._connectTimer);
socket.on('disconnect',function(){
console.log("User disconnected!" + socket.id, new Date());
socket.leave('DTG');
});
});
И это. java file
private void connectSocket() {
try {
Log.i("Emitter", "CONNECT START");
socket = IO.socket("http://192.168.200.121:3004/notifications?userId="+globalUserId+"&companyname="+companyname);
socket.connect();
Log.i("Emitter", "Start");
socket.on("log", log);
} catch (Exception e) {
Log.i(TAG, "SOCKETIO Exception problēma " + e.toString());
}
}
private void disconnectSocket() {
try {
Log.i(TAG, "SOCKETIO EXCEPTION STOP");
socket.off("log", log);
socket.disconnect();
socket = null;
} catch (Exception e) {
Log.i(TAG, "SOCKETIO EXCEPTION problēma");
}
}
private Emitter.Listener log = new Emitter.Listener() {
@Override
public void call(Object...args) {
Log.i("Emitter", "Jauni dati no servera");
// JSONObject laiks = new JSONObject(laiki);
try {
JSONArray array = new JSONArray(args);
JSONObject jb = array.getJSONObject(0);
arrayfinal = jb.getJSONArray("laiki");
for (int j = 0; j < arrayfinal.length(); j++) {
jobject = arrayfinal.getJSONObject(j);
date = jobject.getString("time");
react = jobject.getString("reactionTime");
// Log.i("Emitter", "SOCKETIO setup laiks " + date + " reactionTime: " + react );
}
Log.i("Emitter", "SOCKETIO setup reize " + jobject.toString());
} catch (JSONException e) {
}
}
};
Заранее спасибо, ищите полезную информацию:)