it's possible using javascript socket client in unity js script..?


Question

is the unity js script support for socket client using javascript ? or any something suggestion to use javascript socket client in unity?

i try socket.io library for javascript socket client but i don't know how to implement that in unity, any someone can explain that?

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>

this code in htm page, i think i have idea to include taht script in unity but i can't found documentation for that...thanks for helping

1
1
4/3/2012 3:35:49 AM

Accepted Answer

Unity's javascript is not real javascript. Unity's javascript is a different language called UnityScript, whose syntax is similar to original javascript.

UnityScript is based on Mono, a open source .Net framework implementation. so what you need is .Net socket, not socket.io.

http://msdn.microsoft.com/en-us/library/system.net.sockets.aspx

http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx

For example, to create a TCP client socket in C#:

TcpClient client = new TcpClient(server, port);
NetworkStream stream = client.GetStream();
stream.Write(data, 0, data.Length);

In UnityScript:

var client : TcpClient = new TcpClient(server, port);
var stream : NetworkStream = client.GetStream();
stream.Write(data, 0, data.Length);

Different syntax, but the same API/library.

1
11/12/2015 4:38:40 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon