当服务器的端点被命中时,如何通过服务器的套接字连接发送消息?

时间:2019-12-09 07:14:37

标签: node.js socket.io

我需要通过在碰到服务器端点时已经创建的套接字连接来发送消息,这是怎么做的,因为我无法直接发送消息,因为建立套接字连接的客户端位于本地主机上

编辑-错字更正

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);


io.on('connection', function (socket){
   console.log('connection');
   socket.on('CH01', function (from, msg) {
   console.log('MSG', from, ' saying ', msg);
   socket.emit('CH01', 'me', 'test from server msg');

  });


});


app.post('/getUser', (req, res) => {
    return res.status(200).send({
      success: 'false',
    message: 'title is required'

    });
  })


http.listen(9000, function () {
  console.log('listening on *:9000');
});

1 个答案:

答案 0 :(得分:2)

以上代码正确,但是public class EventsAdapter extends ArrayAdapter<String> { private final Activity context; private final ArrayList<String> titleArray; private final ArrayList<String> eventStartArray; private final ArrayList<String> eventFinishArray; private final ArrayList<String> eventGenderArray; private final ArrayList<String> eventAgeArray; private final ArrayList<String> descriptionArray; public EventsAdapter(Activity context, ArrayList<String> titleArray, ArrayList<String> eventStartArray, ArrayList<String> eventFinishArray, ArrayList<String> eventGenderArray, ArrayList<String> eventAgeArray, ArrayList<String> descriptionArray) { super(context, R.layout.events_list_view, titleArray); this.context = context; this.titleArray = titleArray; this.eventStartArray = eventStartArray; this.eventFinishArray = eventFinishArray; this.eventGenderArray = eventGenderArray; this.eventAgeArray = eventAgeArray; this.descriptionArray = descriptionArray; } public View getView(int position, View view, ViewGroup parent) { LayoutInflater inflater = context.getLayoutInflater(); View rowView = inflater.inflate(R.layout.events_list_view, null, true); TextView eventTitle = rowView.findViewById(R.id.eventTitle); TextView eventDetails = rowView.findViewById(R.id.eventDetails); TextView eventDescription = rowView.findViewById(R.id.eventDescription); eventTitle.setText(titleArray.get(position)); String eventDetailsConcat = eventStartArray.get(position) + " - " + eventFinishArray.get(position) + ", " + eventGenderArray.get(position) + ", " + eventAgeArray.get(position); eventDetails.setText(eventDetailsConcat); eventDescription.setText(descriptionArray.get(position)); return rowView; } } 中有错字。

socket1.emit('CH01', 'me', 'test from server msg')应该是socket1.emitsocket.emit变量中获取套接字对象时,socket未定义。