如何解决React + socket.io-无法在用户之间发出消息
错误:对于单个用户来说,它工作正常,但是我使用另一个Chrome标签页来加入相同的“房间”,它不显示其他人的消息(好像它们不存在)。 我很确定有一个小错误。
使用上下文API来管理房间和名称的状态。
您可以查看完整的代码https://loki.js.org/configuration.html
客户:
io.on('connection',socket => {
socket.on('join',({ name,room },callback: (params: any) => void) => {
const data = addUser({ id: socket.id,name,room });
if (data.error) {
return callback({ error: data.error })
}
socket.join(data.user!.room);
socket.emit('message',{
user: 'admin',text: `${data.user!.name} welcome to the chat...`
});
io.to(data.user!.room).emit('roomData',{
room: data.user!.room,users: getUsersInRoom(data.user!.room)
})
});
socket.on('sendMessage',(message,callback: (params: any) => void) => {
const data = getUser(socket.id);
if (data.error) {
return callback({ error: data.error })
}
io.to(data.user!.room).emit('message',{
user: data.user!.name,text: message
});
});
});
服务器:
yVals1 = new ArrayList<>();
yVals2 = new ArrayList<>();
yVals3 = new ArrayList<>();
for (int i = 0; i < amountByDateList.size(); i++) {
yVals1.add(new Entry(i,amountByDateList.get(i).getPurchases()));
}
for (int i = 0; i < amountByDateList.size(); i++) {
yVals2.add(new Entry(i,amountByDateList.get(i).getBonuses()));
}
for (int i = 0; i < amountByDateList.size(); i++) {
yVals3.add(new Entry(i,amountByDateList.get(i).getWriteOff()));
}
List<String> xValList = new LinkedList<>();
for (int i = 0; i < amountByDateList.size(); i++) {
xValList.add(amountByDateList.get(i).getChequeDate());
}
mLineChart.setOnChartValueSelectedListener(this);
mLineChart.setTouchEnabled(true);
mLineChart.setDragDecelerationFrictionCoef(0.9f);
mLineChart.setDragEnabled(true);
mLineChart.getDescription().setEnabled(false);
mLineChart.setScaleEnabled(true);
mLineChart.setDrawGridBackground(false);
mLineChart.setHighlightPerDragEnabled(false);
mLineChart.setPinchZoom(true);
mLineChart.setAutoScaleMinMaxEnabled(true);
mLineChart.setBackgroundColor(getResources().getColor(R.color.white));
mLineChart.animateY(1000);
mLineChart.setVisibleXRangeMinimum(1);
mLineChart.setLayerType(View.LAYER_TYPE_NONE,null);
mLineChart.getRenderer().getPaintRender().setShadowLayer(10,20,getColorWithAlpha(Color.GRAY,0.1f));
mLineChart.notifyDataSetChanged();
mLineChart.invalidate();
XAxis xAxis = mLineChart.getXAxis();
xAxis.setTextSize(11f);
xAxis.setTextColor(getResources().getColor(R.color.line_chart_color));
xAxis.setDrawGridLines(false);
xAxis.setDrawAxisLine(false);
xAxis.setGranularity(1);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setValueFormatter(new ClaimsXAxisValueFormatter(xValList));
xAxis.setLabelRotationAngle(-70);
YAxis leftAxis = mLineChart.getAxisLeft();
leftAxis.setTextColor(ColorTemplate.getHoloBlue());
leftAxis.setAxisMinimum(0f);
leftAxis.setDrawGridLines(false);
leftAxis.setGranularityEnabled(true);
leftAxis.setEnabled(false);
YAxis rightAxis = mLineChart.getAxisRight();
rightAxis.setTextColor(Color.RED);
rightAxis.setAxisMinimum(0);
rightAxis.setDrawGridLines(false);
rightAxis.setEnabled(false);
rightAxis.setDrawZeroLine(false);
rightAxis.setGranularityEnabled(false);
mLineChart.getLegend().setEnabled(false);
LineDataSet set1,set2,set3;
set1 = new LineDataSet(yVals1,"");
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
set1.setColor(getResources().getColor(R.color.shopping_amount_color));
set1.setCircleColor(getResources().getColor(R.color.shopping_amount_color));
set1.setLineWidth(4f);
set1.setCircleRadius(1f);
set1.setFillAlpha(60);
set1.setFillColor(getResources().getColor(R.color.shopping_amount_color));
set1.setHighLightColor(getResources().getColor(R.color.shopping_amount_color));
set1.setDrawCircleHole(false);
set2 = new LineDataSet(yVals2,"");
set2.setAxisDependency(YAxis.AxisDependency.RIGHT);
set2.setColor(getResources().getColor(R.color.filled_up_rewards_color));
set2.setLineWidth(4f);
set2.setCircleRadius(1f);
set2.setFillAlpha(60);
set2.setFillColor(getResources().getColor(R.color.filled_up_rewards_color));
set2.setDrawCircleHole(false);
set2.setCircleColor(getColorWithAlpha(getResources().getColor(R.color.filled_up_rewards_color),0.4f));
set2.setHighLightColor(getResources().getColor(R.color.filled_up_rewards_color));
set3 = new LineDataSet(yVals3,"");
set3.setAxisDependency(YAxis.AxisDependency.RIGHT);
set3.setColor(getResources().getColor(R.color.write_off_rewards_color));
set3.setLineWidth(4f);
set3.setCircleRadius(1f);
set3.setFillAlpha(60);
set3.setFillColor(getResources().getColor(R.color.write_off_rewards_color));
set3.setDrawCircleHole(false);
set3.setCircleColor(getColorWithAlpha(getResources().getColor(R.color.write_off_rewards_color),0.4f));
set3.setHighLightColor(getResources().getColor(R.color.write_off_rewards_color));
set1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
set2.setMode(LineDataSet.Mode.CUBIC_BEZIER);
set3.setMode(LineDataSet.Mode.CUBIC_BEZIER);
LineData data = new LineData(set3,set1);
data.setValueTextColor(getResources().getColor(R.color.transparent));
mLineChart.clear();
if (null != mLineChart.getLineData())
mLineChart.getLineData().clearValues();
mLineChart.setData(data);
mLineChart.getData().notifyDataChanged();
mLineChart.notifyDataSetChanged();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。