Sockets.io连接,但不发送消息

时间:2016-08-30 12:03:40

标签: javascript java sockets angular ionic2

我有一个Java服务器,用于侦听Ionic 2客户端上的消息。我可以让客户端和服务器通过Sockets.io连接,我可以让服务器成功地向客户端发送消息。但是,我无法让客户端向服务器发送消息。

即。调用了javascript send(message),它应该emit消息到服务器,但它不是("onSend"永远不打印)。但是"Welcome to the chat!"消息正在从服务器成功发送到客户端。

欢迎任何建议吗?

Java(服务器)

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");

    }
}

Ionic 2 TypeScript(客户端)

import { Component, NgZone } from '@angular/core';
import { Http } from "@angular/http";
declare var io;
//require ('io');

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

export class ChatPage {

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

  constructor(http: Http) {
    this.messages = [];
    this.zone = new NgZone({ enableLongStackTrace: false });
    let url = this.socketHost + "/fetch";
    //let url = this.socketHost;
    http.get(url).subscribe((success) => {
      var data = success.json();
      for (var i = 0; i < data.length; i++) {
    console.log('sub: '+data[i].message);
        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", (msg) => {
      this.zone.run(() => {
    console.log('run: '+msg);
    console.log(msg);
        this.messages.push(msg);
      });
    });
  }

  send(message) {
    if (message && message != "") {
    console.log('send: '+message);
      this.socket.emit("chat_message:send", message);
    }
    this.chatBox = "";
  }
}

1 个答案:

答案 0 :(得分:1)

我的错误。应发送消息对象。我试图发送一个字符串