如何使用Ionic客户端连接到C#服务器

时间:2017-02-16 10:06:50

标签: c# ionic-framework server client

我有一个c#服务器。我需要使用离子客户端连接到此服务器。 我在哪里犯错误。我无法通过Ionic客户端连接到C#服务器。 这是可能的。我不知道。我很困惑。我在等你的帮助。 我期待着你和你的想法。

C#服务器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.IO;

 public class ServerSocket1
{
public static void Main()
{
    try
    {
        bool status = true;
        string servermessage = "";
        string clientmessage = "";
        TcpListener tcpListener = new TcpListener(2100);
        tcpListener.Start();
        Console.WriteLine("Server Started");
        Socket socketForClient = tcpListener.AcceptSocket();
        Console.WriteLine("Client Connected");
        NetworkStream networkStream = new NetworkStream(socketForClient);
        StreamWriter streamwriter = new StreamWriter(networkStream);
        StreamReader streamreader = new StreamReader(networkStream);
        while (status)
        {
            if (socketForClient.Connected)
            {
                servermessage = streamreader.ReadLine();
                Console.WriteLine("Client:" + servermessage);
                if ((servermessage == "bye"))
                {
                    status = false;
                    streamreader.Close();
                    networkStream.Close();
                    streamwriter.Close();
                    return;
                }
                Console.Write("Server:");
                clientmessage = Console.ReadLine();
                streamwriter.WriteLine(clientmessage);
                streamwriter.Flush();
            }
        }
        streamreader.Close();
        networkStream.Close();
        streamwriter.Close();
        socketForClient.Close();
        Console.WriteLine("Exiting");
    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
    }
}
}

离子客户

service.js

angular.module('starter.services', ['btford.socket-io'])


  .factory('mySocket', function (socketFactory) {
   var myIoSocket = io.connect('http://l92.168.1.27:2100');


mySocket = socketFactory({
    ioSocket: myIoSocket
});

return mySocket;
  })

controller.js

angular.module('starter.controllers', [])

.controller('DashCtrl', function ($scope, mySocket) {

$scope.data = {};


document.addEventListener('deviceready', function () {

    mySocket.on('connect', function () {

        mySocket.on('Received a message from the server!', function (data) {
            alert(data);
        });
    });
      });

0 个答案:

没有答案