简体   繁体   English

用angular 2中的socket.io客户端替换Java套接字客户端

[英]Replace java socket client with the socket.io client in angular 2

I am currently connecting to a server and a message to server using java client socket, I want to do the same using socket.io in my angular 2 application. 我目前正在使用Java客户端套接字连接到服务器,并向服务器发送消息,我想在我的angular 2应用程序中使用socket.io进行相同操作。 I tried as in the below code but no luck in sending the message, any suggestion. 我尝试了下面的代码,但是发送消息没有运气,没有任何建议。 Please help. 请帮忙。

JAVA: JAVA:

public String triggerMessage() {
             String ipServer = "localhost";
        int port = 5555;
        String token="";
        try {
            Socket socket= new Socket(ipServer, port);
            DataOutputStream dataOut = new DataOutputStream(socket.getOutputStream());
            dataOut.writeUTF("[START]");
            BufferedReader resReader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
            String str;
            while ((str = resReader.readLine()) != null) {
                if(str.contains("TOKEN")){
                    token = str;
                }
            }
            dataOut.close();
        } catch (Exception e) {
            log.error(e);
        }
        return token;

    }

ANGULAR 2 (Socket.io): 角度2(Socket.io):

below is the service written in angular 2 (typescript) to send the message to the server. 下面是用angular 2(打字稿)编写的用于将消息发送到服务器的服务。

import { Subject } from 'rxjs/Subject';
import { Observable } from 'rxjs/Observable';
import * as io from 'socket.io-client';

export class SocketService {
  startDevice(){
  this.socket = io('https://localhost:5555');
  this.socket.emit('message', '[START]');
  }
}

Can any one suggest on what is wrong here? 有人可以建议这里有什么问题吗?

Just Found that socket.io can be used only for connection between socket.io client and its server. 刚发现,socket.io只能用于socket.io客户端与其服务器之间的连接。 How to connect to a telnet server from node using socket.io 如何使用socket.io从节点连接到telnet服务器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM