SuperSocket 1.4系列文档(17) 在Windows Azure中运行SuperSocket

本文详细介绍了如何将基于SuperSocket的服务器程序部署到WindowsAzure平台,并通过设置Socket程序对外提供服务的Endpoint来实现跨平台通信。文章提供了完整的代码示例,帮助开发者轻松地将应用迁移到云端。

Windows Azure是微软的云计算平台!Windows Azure通过微软的数据中心为开发人员提供以按需的计算能力和存储能力去托管、扩展和管理互联网上的应用程序。

运行于Windows Azure上的应用程序具有很高的可靠性和可伸缩性。

基于SuperSocket的服务器程序可以轻易的运行于Windows Azure平台之上。

和普通Socket服务器程序不同,首先需要在Role的属性中设置Socket程序对外提供服务的Endpoint:

M[90I@W~C78O1JN{4D@S@$9[4]

然后在WorkerRole代码文件中动态的获取外部Endpoint所对应的内部Endpoint:

var instanceEndpoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[serverConfig.Name + "Endpoint"];

完整的代码如下:

public override bool OnStart()
{
    LogUtil.Setup();
    // Set the maximum number of concurrent connections 
    ServicePointManager.DefaultConnectionLimit = 100;
 
    // For information on handling configuration changes
    // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
 
    var serverConfig = ConfigurationManager.GetSection("socketServer") as SocketServiceConfig;
 
    if (!SocketServerManager.Initialize(serverConfig, ResolveServerConfig))
    {
        Trace.WriteLine("Failed to initialize SuperSocket!", "Error");
        return false;
    }
 
    if (!SocketServerManager.Start())
    {
        Trace.WriteLine("Failed to start SuperSocket!", "Error");
        return false;
    }
 
    return base.OnStart();
}
 
private IServerConfig ResolveServerConfig(IServerConfig serverConfig)
{
    var config = new ServerConfig();
    serverConfig.CopyPropertiesTo(config);
 
    var instanceEndpoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[serverConfig.Name + "Endpoint"];
    if (instanceEndpoint == null)
    {
        Trace.WriteLine(string.Format("Failed to find Input Endpoint configuration {0}!", serverConfig.Name + "Endpoint"), "Error");
        return serverConfig;
    }
 
    var ipEndpoint = instanceEndpoint.IPEndpoint;
    config.Ip = ipEndpoint.Address.ToString();
    config.Port = ipEndpoint.Port;
    return config;
}

就这样,你的SuperSocket服务器程序就可以正确的运行在Windows Azure平台之上。

完整的示例代码,请参考源代码中WindowsAzure文件夹下的项目。

转载于:https://www.cnblogs.com/jzywh/archive/2011/05/16/2048131.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值