How to use Socket.IO Client .NET

i am creating a new chat Messenger but i am not understand how to use Socket.IO Client .NET in my app plz Help

Posts

  • tribaltribal USMember ✭✭✭

    Do you have any specific reason for using the Socket .IO library. There seems to be a an implementation of it for C#(socketio4net) but its not maintained now. Not sure if its PCL compatible either.

    There are a few others for .NET as well notably Alchemy-Websockets & SuperWebSocket but I am not sure if they're PCL or .NET Standard compliant which you will need for using in Xamarin.

    By the way there are also a couple of open source Xamarin Chat applications but they're Azure/Twilio based

    MonkeyChat

    SnapChat

  • SinghsumitSinghsumit INMember ✭✭

    @tribal Very Thanks, Alchemy-Websockets & SuperWebSocket free or Not.

  • tribaltribal USMember ✭✭✭

    @Singhsumit both are open source and free.

  • SinghsumitSinghsumit INMember ✭✭

    @tribal thanks :smile:

  • AjaysinhDodiyaAjaysinhDodiya INMember ✭✭
    edited March 2018

    @Singhsumit
    Alchemy-Websockets & SuperWebSocket is compatible with .Net standard

  • joscojosco Member ✭✭

    I am using this Nuget:
    https://components.xamarin.com/view/socketioclientdotnet

    My solution is this.
    I Have an api with node js and my app.js looks like this:

    var express = require('express');
    var bodyParser = require('body-parser');
    
    // create express app
    var app = express();
    var fs = require('fs');
    
    // parse requests of content-type - application/x-www-form-urlencoded
    app.use(bodyParser.urlencoded({ extended: true }))
    
    // parse requests of content-type - application/json
    app.use(bodyParser.json())
    
    app.use(express.static(__dirname + '/public'));
    
    // Configuring the database
    var dbConfig = require('./config/database.config.js');
    var mongoose = require('mongoose');
    
    mongoose.Promise = global.Promise;
    
    mongoose.connect(dbConfig.url, {
        useMongoClient: true
    });
    
    mongoose.connection.on('error', function() {
        console.log('Could not connect to the database. Exiting now...');
        process.exit();
    });
    
    mongoose.connection.once('open', function() {
        console.log("Successfully connected to the database");
    })
    
    // define a simple route
    app.get('/', function(req, res){
        //res.json({"message": "Welcome to MexiRoomies application. Take notes quickly. Organize and keep track of all your notes."});
    
       //Telling Browser That The File Provided Is A HTML File
      res.writeHead(200,{"Content-Type":"text/html"});
      //Passing HTML To Browser
      res.write(fs.readFileSync("./public/index.html"));
      //Ending Response
      res.end();
    });
    
    // Require Posts routes
    require('./app/routes/post.routes.js')(app);
    // Require Users routes
    require('./app/routes/user.routes.js')(app);
    // Require Conversations routes
    require('./app/routes/conversation.routes.js')(app);
    
    // listen for requests
    /*app.listen(3000, function(){
        console.log("Server is listening on port 3000");
    });
    */
    var io = require('socket.io').listen(app.listen(3000));
    
    
    //For Tracking When User Connects:
    io.sockets.on("connection",function(socket){
    //var socket is the socket for the client who has connected.
        console.log("new connection");
    })
    //For Tracking When User Disconnects:
    io.sockets.on("disconnect",function(socket){
        //var socket is the socket for the client who has disconnected.
        console.log("disconnected");
    })
    

    And my class for connect in xamarin pcl looks like this:

    using System;
    using Quobject.SocketIoClientDotNet.Client;
    
    namespace MexiRoomies.SocketIO
    {
    
    
        public class Client
        {
    
            public Client(string pIpAddress, int port)
            {
                var socket = IO.Socket("http://"+pIpAddress+":"+port);
                socket.On( Socket.EVENT_CONNECT, () =>
                {
                    socket.Emit("hi");
    
                });
    
                socket.On("hi", (data) =>
                {
                    Console.WriteLine(data);
                    socket.Disconnect();
                });
                Console.ReadLine();
            }
    
    
    
        }
    }
    

    So when I instantiate my client class

    new Client(Constants.MEXIROOMIES_API_HOST_NAME, Constants.MEXIROOMIES_API_PORT);

    where Constants.MEXIROOMIES_API_HOST_NAME = "1.45.213.34" change this for your hostname without the http//:
    and Constants.MEXIROOMIES_API_PORT = 3000 change 3000 for your host port

    the result in the node js server console looks like this:

  • amou43amou43 Member

    @josco thank you for the example.
    I tried to do the same, i add the SocketioClientDotNet Nuget in my common xamarin projet and when i try to lauch it on my iphone i get : "Could not link assemblies"
    Thank you
    Amaury

  • joscojosco Member ✭✭

    @amou43 said:
    @josco thank you for the example.
    I tried to do the same, i add the SocketioClientDotNet Nuget in my common xamarin projet and when i try to lauch it on my iphone i get : "Could not link assemblies"
    Thank you
    Amaury

    Hi, try with 1.0.5 version on all your projects

    this is how my project looks like:

    this is how ios build settings looks like:

    I have to mention that my project is not using code sharing strategy .Net standard , it is using Shared project strategy and I haven't tested this nuget with .Net standar but a believe that should work also with .Net standar

Sign In or Register to comment.