Sockets.io接收但不发送消息

时间:2016-08-30 15:07:18

标签: javascript java sockets angular ionic2

我正在构建一个聊天应用。我有一个客户端(Ionic 2)与服务器(Java)交谈。客户端可以从服务器成功接收消息(例如,在连接时收到"Welcome to the chat!"),但是当我尝试从客户端向服务器发送消息时,它不会到达那里(DataListener<Message>不是在"chat_message:send"事件上被调用。)。

请提出任何建议?

服务器代码:

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.listener.ConnectListener;
import com.corundumstudio.socketio.listener.DataListener;
import com.corundumstudio.socketio.listener.DisconnectListener;

public class Server {

    public static void main(String[] args) {
        Configuration config = new Configuration();
        config.setHostname("localhost");
        config.setPort(3700);
        final SocketIOServer server = new SocketIOServer(config);

        server.addConnectListener(new ConnectListener() {
            @Override
            public void onConnect(SocketIOClient client) {
                System.out.println("onConnected");
                client.sendEvent("chat_message:message", new Message("Welcome to the chat!"));
            }
        });

        server.addDisconnectListener(new DisconnectListener() {
            @Override
            public void onDisconnect(SocketIOClient client) {
                System.out.println("onDisconnected");
            }
        });

        server.addEventListener("chat_message:send", Message.class, new DataListener<Message>() {
            @Override
            public void onData(SocketIOClient client, Message data, AckRequest ackSender) throws Exception {
                System.out.println("onSend: " + data.toString());
                server.getBroadcastOperations().sendEvent("chat_message:message", data);
            }
        });

        System.out.println("Starting server...");
        server.start();
        System.out.println("Server started");

    }
}

public class Message {
    private String message;

    public Message(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}

客户代码:

import { Component, NgZone } from '@angular/core';
import { Http } from "@angular/http";
import { MessageModel } from '../model/messageModel';
import { UtilityService } from '../utils/utilityService';
declare var io;

@Component({
  templateUrl: 'build/pages/chat/chat.html',
  providers: [UtilityService]
})

export class ChatPage {

  private socketHost: string = "http://localhost:3700";
  private messages: MessageModel[] = [];
  private zone: NgZone = null;
  private chatBox: string = null;
  private socket: any = null;
  private utilityService: UtilityService = null;

  constructor(http: Http, utilityService: UtilityService) {
    this.utilityService = utilityService;
    this.setUpMessageService(http);
  }

  setUpMessageService(http: Http): void {
    this.messages = [];
    this.zone = new NgZone({ enableLongStackTrace: false });
    let url = this.socketHost + "/fetch";

    http.get(url).subscribe((success) => {
      var data = success.json();
      console.log(data);
      for (var i = 0; i < data.length; i++) {
        this.messages.push(data[i].message);
      }
    }, (error) => {
      console.log(JSON.stringify(error));
    });
    this.chatBox = "";

    this.socket = io(this.socketHost);
    this.socket.on("chat_message:message", (messageModel: MessageModel) => {
      this.zone.run(() => {
        console.log('run: ' + messageModel);
        this.messages.push(messageModel);
      });
    });
  }

  send(msg) {
    if (msg && msg != "") {
      let messageModel: MessageModel = new MessageModel();
      messageModel.message = msg;
      console.log(messageModel);
      this.socket.emit("chat_message:send", messageModel);
    }
    this.chatBox = "";
  }
}

import { Injectable } from "@angular/core";

    @Injectable()
    export class MessageModel {
            public message: string = null;
    }

1 个答案:

答案 0 :(得分:0)

如果我将消息作为字符串而不是来自客户端的MessageModel发送,则可以正常工作。