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)
}
}
}
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 courseimport net from 'net'in the server side and I use websocket in the browser, Can I solve this problem ? @Bravo'net'for tcp server. I also gonna usesocket.ioin the server side. I gonna usesocket.io-clientin the browser too. after if data comes from tcp server, I gonna send data to browser withsocket.io. when I want to send data to server from browser, I gonna usesocket.io-client. I dont know, it gonna able to be.