TCP Socket with Javascript

Hi all,
I’m new to fuse and I’m looking to create a TCP connection within Javascript.

I saw Uno.Net.Sockets but not sure it can be called with Javascript !

There is sample somewhere about how to use Uno.Net.Sockets ?

Thank you for your help.

We have not exposed this api to JavaScript yet. You can still use this from Uno and expose it your self to JS.

Here my MySocket.uno code

using Fuse;
using Fuse.Scripting;
using Fuse.Reactive;
using Uno.UX;
using Uno.Net.Sockets;
using Uno.Text;
using Uno.Threading;


[UXGlobalModule]
public class MySocket : NativeModule
{
    static readonly MySocket _instance;
    
    NativeEvent _nativeEvent;
    NativeEvent _nativeEventConnected;
    NativeEvent _nativeEventError;
    Socket socket;

    public MySocket()
    {
        // Make sure we're only initializing the module once
        if(_instance != null) return;
        _instance = this;
        Resource.SetGlobalKey(_instance, "MySocket");
        AddMember(new NativeFunction("connect", (NativeCallback)Connect));
        AddMember(new NativeFunction("send", (NativeCallback)Send));
        AddMember(new NativeFunction("read", (NativeCallback)Read));
        
        _nativeEvent = new NativeEvent("onMessageReceived");
        AddMember(_nativeEvent);
        _nativeEventConnected = new NativeEvent("onConnected");
        AddMember(_nativeEventConnected);
        _nativeEventError = new NativeEvent("onError");
        AddMember(_nativeEventError);

    }

    string Send(Context c, object[] args)
    {
        var message = args[0] as string;
        var connected = socket.Connected;
        debug_log(connected);
        socket.Send(Ascii.GetBytes(message));
        debug_log(message);
        _nativeEvent.RaiseAsync("SENT");
        return "";
    }

    string Connect(Context c, object[] args)
    {
        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);       
        var address = args[0] as string;
        debug_log address;
        int port = int.Parse(args[1] as string);
        debug_log port;
        debug_log socket;
        try {
          socket.Connect(address, port);
          _nativeEventConnected.RaiseAsync("Connected");
          
        } catch(SocketException e) {
          _nativeEventError.RaiseAsync(e.Message);
        }
        return "";
    }

    string Read(Context c, object[] args)
    {
        var read = new byte[] {};
        socket.Receive(read);
        debug_log("Read data : " + Ascii.GetString(read));
        _nativeEvent.RaiseAsync(Ascii.GetString(read));
        return "";
    }
    

    
}

The Connect method is working fine, the Send method too … but my Read method return nothing (always blank)

Here the view :

<App>
    <DockPanel>
        <StatusBarBackground Dock="Top" />
        <ScrollView ClipToBounds="true">
            <StackPanel>
                <Text FontSize="30" Alignment="Center">Hello, world!</Text>
                <Button Text="Send DETECT" Clicked="{onClick}"/>
                <Button Text="Read Buffer" Clicked="{onClickRead}"/>
        
                
                
            </StackPanel>
        </ScrollView>
        <JavaScript>
          var Socket = require('MySocket');
          var Observable = require('FuseJS/Observable');
          var buttonText = Observable('Button');
          
          Socket.connect("127.0.0.1", "8888");
          
          Socket.onError = function(message) {
            console.log(message);
          }
          Socket.onConnected = function(message) {
            console.log(message);
          }
          Socket.onMessageReceived = function(message) {
            console.log(message);
            if(message == "SENT") {
              Socket.read();
            }
          }
          
          function onClick() {
             Socket.send("DETECT\n");
          }

          function onClickRead() {
             Socket.read();
          }

          module.exports = {
              buttonText: buttonText,
              onClick: onClick,
              onClickRead: onClickRead,
          }
      </JavaScript>
    </DockPanel>
</App>

The server is a nodeJS TCP server which works fine (tested with a ruby client)

Any thought about my .uno file ? How to run a loop to catch any new data in the Socket ? I’m a newbie on C#

Hi!

Our socket API is based on the C# socket API. My suggestion is to have a look at the C# documentation and examples for sockets https://msdn.microsoft.com/en-us/library/w89fhyex(v=vs.110).aspx

Well, I can’t see the BeginReceive method … I don’t see where to set the callback method for reads.

Looks like it differ a bit from the C# version …:frowning:

Ok I have found how to read data … Now I need to find how to run the read inside a Thread … Do you have any Thread code sample within UNO ?

Threading in Uno is very similar to C# 2.0 even though we don’t have all the high level methods (like BeginReceive), it should be possible to read up on the general documentation.

I did write a simple project which works fine now … I hope it will help beginners

Any thoughts will be welcome :slight_smile: