1

I try to create a tcp client in vue.js. I use this code below. but it gives to me error. that error is net.Socket() is not a constructor. How can I solve this problem or How can I create a client for tcp with another way. help please.

import net from 'net'

export default {
  data() {
    return {
      ip: '192.168.1.118',
      port: '8085',
      message: 'test message',
      socket: null
    }
  },
  mounted() {
    const socket = new net.Socket()
    this.socket = socket
    socket.on('data', (data) => console.log(data.toString()))
  },
  methods: {
    connect() {
      this.socket.connect(this.port, this.ip)
    },
    sendMessage() {
      this.socket.write(this.message)
    }
  }
}
6
  • 1
    import net from 'net' is available on node.js which is server side - browsers aren't node.js - you can't use server side only API's in a browser - and really, this has nothing to do with vue.js, just browsers in general - you may be able to achieve something with websockets - depending on the server side of course Commented Dec 4, 2020 at 6:49
  • so if I use import net from 'net' in the server side and I use websocket in the browser, Can I solve this problem ? @Bravo Commented Dec 4, 2020 at 6:56
  • reading this may help you: stackoverflow.com/questions/40599069/… Commented Dec 4, 2020 at 7:28
  • I'm gonna try to use 'net' for tcp server. I also gonna use socket.io in the server side. I gonna use socket.io-client in the browser too. after if data comes from tcp server, I gonna send data to browser with socket.io. when I want to send data to server from browser, I gonna use socket.io-client. I dont know, it gonna able to be. Commented Dec 4, 2020 at 7:42
  • @SefaUn - sure, give it a go Commented Dec 4, 2020 at 7:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.