2012-01-12 18:19 js通过as完成socket通信

本文介绍了一种使用JavaScript和ActionScript实现跨平台Socket通信的方法。通过Flash作为桥梁,利用其强大的Socket支持能力,实现浏览器端与服务器端的数据交互。文章详细展示了如何设置和使用Socket连接,包括连接建立、发送数据、接收数据以及错误处理等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

js通过as完成socket通信

[ as ] :

===================================================

import flash.external.ExternalInterface;

import flash.net.Socket;

import flash.events.SecurityErrorEvent;

import flash.events.IOErrorEvent;

import flash.events.ProgressEvent;

var mySocket:Socket = new Socket();

mySocket.addEventListener(Event.CONNECT,connectHandler);

mySocket.addEventListener(Event.CLOSE,closeHandler);

mySocket.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);

mySocket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,seurityErrorHandler);

mySocket.addEventListener(ProgressEvent.SOCKET_DATA,socketDataHandler);

function connectHandler(e:Event):void{

ExternalInterface.call("flashEvent.connectHandler");

}

function closeHandler(e:Event):void{

ExternalInterface.call("flashEvent.closeHandler");

}

function ioErrorHandler(evt:IOErrorEvent):void{

ExternalInterface.call("flashEvent.ioErrorHandler");

}

function seurityErrorHandler(evt:SecurityErrorEvent):void{

ExternalInterface.call("flashEvent.seurityErrorHandler");

}

function socketDataHandler(evt:ProgressEvent):void{

var msg:String = "";

while(mySocket.bytesAvailable)

{

msg += mySocket.readMultiByte(mySocket.bytesAvailable,"utf-8");

}

ExternalInterface.call("flashEvent.socketDataHandler",msg);

}

function sendData(str:String):void{

mySocket.writeUTFBytes(str);

mySocket.flush();

}

function connectSocket(host:String,port:String):void{

mySocket.connect(host,int(port));

}

function closeSocket(str:String):void{

mySocket.close();

}

ExternalInterface.addCallback("sendDataFromAs", sendData);

ExternalInterface.addCallback("closeSocketFromAs", closeSocket);

ExternalInterface.addCallback("connectSocketFromAs", connectSocket);

ExternalInterface.call("flashEvent.asReady");

[ js ] :

==================================================================

var flashSocketNode = Y.Node.create('<embed src="flash/flash_socket.swf" width="1" height="1" allowScriptAccess="sameDomain" type="application/x-shockwave-flash">').appendTo(document.body),

flashSocketNode = Y.Node.getDOMNode(flashSocketNode),

asReady = false,

that = this;

this._flashSocket = {

isAsReady : function(){

return asReady;

},

connect : function(host,port){

flashSocketNode.connectSocketFromAs(host,port);

},

close : function(){

flashSocketNode.closeSocketFromAs();

},

send : function(data){

flashSocketNode.sendDataFromAs(data);

},

onopen : function(){},

onclose : function(){},

onmessage : function(){}

}

window.flashEvent = {

asReady : function(){

asReady = true;

},

connectHandler : function(){

that._flashSocket.onopen();

},

closeHandler : function(){

that._FlashSocket.onclose();

},

ioErrorHandler : function(){

new Y.Win().alert({"content":"flash io error"});

},

seurityErrorHandler : function(){

new Y.Win().alert({"content":"flash seurity error"});

},

socketDataHandler : function(data){

dataList = data.slice(0,-2).split("\r\n");

Y.each(dataList,function(_data){

that._flashSocket.onmessage({data:_data});

if(dataList.length>1){

new Y.Win().alert({content:_data,width:1000,height:500});

}

},that);

}

};



[ server ] :

============================================================

def __handshakeHandler(self,data):

response = ""

if "<policy-file-request" in data :

response = '''

<?xml version="1.0"?>

<cross-domain-policy>

<allow-access-from domain="*" to-ports="*" />

</cross-domain-policy>\0'''.lstrip()


def __wrapResponse(self,str):

response = ""

if self.__getDraftType() == "common":

response = '%s\r\n' % str

================================================ AI系统正在启动 [周一 2025/08/11 22:52:48.99] ================================================ [1/3] 启动模型API服务... 2025-08-11 22:52:49,235 - APIServer - INFO - 开始初始化模型... 2025-08-11 22:52:52,366 - APIServer - CRITICAL - 模型初始化异常: cannot import name 'cached_download' from 'huggingface_hub' (E:\Python310\lib\site-packages\huggingface_hub\__init__.py) ================================================== Starting DeepSeek-7B Model API Server Start Time: 2025-08-11T14:52:52.367935Z Model Status: Not Loaded ================================================== API Endpoints: - Generate: POST http://127.0.0.1:6000/generate - Status: GET http://127.0.0.1:6000/status - Health: GET http://127.0.0.1:6000/health ================================================== * Serving Flask app 'api_server' * Debug mode: off WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on http://127.0.0.1:6000 Press CTRL+C to quit [2/3] 启动AI核心服务... Traceback (most recent call last): File "E:\AI_System\agent\agent_core.py", line 24, in <module> from agent.cognitive_architecture import CognitiveArchitecture File "E:\AI_System\agent\__init__.py", line 5, in <module> from .autonomous_agent import AutonomousAgent # 修改此处 File "E:\AI_System\agent\autonomous_agent.py", line 19, in <module> from core.config import system_config ImportError: cannot import name 'system_config' from 'core.config' (E:\AI_System\core\config.py) [3/3] 启动用户界面... 系统启动完成!自动打开监控页面... ✅ Eventlet monkey patch applied at startup 2025-08-11 22:52:57 [INFO] WebServer: ================================================== 2025-08-11 22:52:57 [INFO] WebServer: 🚀 开始初始化AI系统 2025-08-11 22:52:57 [INFO] WebServer: ================================================== 2025-08-11 22:52:57 [INFO] WebServer: 项目根目录: E:\AI_System 2025-08-11 22:52:57 [INFO] WebServer: 添加路径: E:\AI_System\agent 2025-08-11 22:52:57 [INFO] WebServer: 添加路径: E:\AI_System\core 2025-08-11 22:52:57 [INFO] WebServer: 添加路径: E:\AI_System\utils 2025-08-11 22:52:57 [INFO] WebServer: 添加路径: E:\AI_System\config 2025-08-11 22:52:57 [INFO] WebServer: 添加路径: E:\AI_System\cognitive_arch 2025-08-11 22:52:57 [INFO] WebServer: 添加路径: E:\AI_System\environment 2025-08-11 22:52:57 [INFO] WebServer: ✅ 模拟AI核心初始化 2025-08-11 22:52:57 [INFO] WebServer: ✅ 模拟硬件管理器初始化 2025-08-11 22:52:57 [INFO] WebServer: ✅ 模拟生活调度器初始化 2025-08-11 22:52:57 [INFO] WebServer: ✅ 模拟AI智能体初始化 2025-08-11 22:52:57 [INFO] WebServer: 环境管理器已启动 2025-08-11 22:52:57 [INFO] WebServer: ✅ 环境管理器初始化成功 2025-08-11 22:52:57 [INFO] WebServer: ✅ 模拟进化监视器启动 2025-08-11 22:52:57 [INFO] WebServer: ✅ 所有系统组件初始化完成 2025-08-11 22:52:57 [INFO] WebServer: ✅ 使用eventlet异步模式 2025-08-11 22:52:57 [INFO] WebServer: 🚀 开发服务器启动: http://0.0.0.0:5000 (17256) wsgi starting up on http://0.0.0.0:5000 ================================================ 运行中进程: python.exe 16140 Console 1 210,852 K python.exe 17256 Console 1 50,544 K ================================================ 按任意键退出此窗口(服务将继续在后台运行)... (17256) accepted ('127.0.0.1', 50016) (17256) accepted ('127.0.0.1', 50032) 127.0.0.1 - - [11/Aug/2025 22:53:21] "GET / HTTP/1.1" 200 3308 0.001000 127.0.0.1 - - [11/Aug/2025 22:53:21] "GET /favicon.ico HTTP/1.1" 404 212 0.000000 127.0.0.1 - - [11/Aug/2025 22:53:29] "GET /life/dashboard HTTP/1.1" 200 32013 0.003999 Traceback (most recent call last): File "E:\Python310\lib\site-packages\flask\app.py", line 2193, in wsgi_app response = self.handle_exception(e) File "E:\Python310\lib\site-packages\flask\app.py", line 2190, in wsgi_app response = self.full_dispatch_request() File "E:\Python310\lib\site-packages\flask\app.py", line 1486, in full_dispatch_request rv = self.handle_user_exception(e) File "E:\Python310\lib\site-packages\flask\app.py", line 1484, in full_dispatch_request rv = self.dispatch_request() File "E:\Python310\lib\site-packages\flask\app.py", line 1469, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) File "E:\AI_System\web_ui\server.py", line 467, in status "hardware_status": components['hardware_manager'].get_status() if components[ TypeError: SystemInitializer.initialize_hardware_manager.<locals>.<lambda>() takes 0 positional arguments but 1 was given 127.0.0.1 - - [11/Aug/2025 22:53:42] "GET /status HTTP/1.1" 500 12795 0.012914 127.0.0.1 - - [11/Aug/2025 22:53:42] "GET /status?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 304 183 0.016000 127.0.0.1 - - [11/Aug/2025 22:53:42] "GET /status?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 304 184 0.000000 127.0.0.1 - - [11/Aug/2025 22:53:42] "GET /status?__debugger__=yes&cmd=resource&f=console.png&s=cy8AirLRNcqty2WTg3dx HTTP/1.1" 200 729 0.001000 127.0.0.1 - - [11/Aug/2025 22:53:54] "GET /health HTTP/1.1" 200 203 0.001001 127.0.0.1 - - [11/Aug/2025 22:53:57] "GET /chat HTTP/1.1" 200 2844 0.009460 127.0.0.1 - - [11/Aug/2025 22:53:59] "GET /socket.io/?EIO=4&transport=polling&t=PYPdxlY HTTP/1.1" 200 300 0.000000 2025-08-11 22:53:59 [INFO] WebServer: 客户端已连接 127.0.0.1 - - [11/Aug/2025 22:53:59] "POST /socket.io/?EIO=4&transport=polling&t=PYPdxla&sid=y_bOfyPWaorXA7sEAAAA HTTP/1.1" 200 219 0.001000 (17256) accepted ('127.0.0.1', 50075) 127.0.0.1 - - [11/Aug/2025 22:53:59] "GET /socket.io/?EIO=4&transport=polling&t=PYPdxlb&sid=y_bOfyPWaorXA7sEAAAA HTTP/1.1" 200 252 0.000000 127.0.0.1 - - [11/Aug/2025 22:53:59] "GET /socket.io/?EIO=4&transport=polling&t=PYPdxlc&sid=y_bOfyPWaorXA7sEAAAA HTTP/1.1" 200 181 0.000000 127.0.0.1 - - [11/Aug/2025 22:53:59] "GET /socket.io/?EIO=4&transport=polling&t=PYPdxle&sid=y_bOfyPWaorXA7sEAAAA HTTP/1.1" 200 181 0.000000 2025-08-11 22:54:01 [INFO] WebServer: 收到来自 user_1754924041725 的消息: hi 2025-08-11 22:54:08 [INFO] WebServer: 收到来自 user_1754924048779 的消息: 能听见吗 2025-08-11 22:54:13 [INFO] WebServer: 客户端已断开连接 127.0.0.1 - - [11/Aug/2025 22:54:13] "GET /socket.io/?EIO=4&transport=websocket&sid=y_bOfyPWaorXA7sEAAAA HTTP/1.1" 200 0 14.650252 127.0.0.1 - - [11/Aug/2025 22:54:14] "GET /environment HTTP/1.1" 200 47268 0.014014 #E:\AI_System\core\config.py # import os import json import logging import configparser from pathlib import Path from typing import Dict, Any, Optional, Union from dotenv import load_dotenv class CoreConfig: """ 核心配置管理类 - 提供统一的配置管理接口 支持多来源配置加载:环境变量 > 配置文件 > 默认值 """ def __init__(self, config_path: Optional[Union[str, Path]] = None, env_prefix: str = "APP_", default_config: Optional[Dict[str, Any]] = None): """ 初始化配置管理器 :param config_path: 配置文件路径(支持.json, .ini, .env) :param env_prefix: 环境变量前缀 :param default_config: 默认配置字典 """ self.logger = logging.getLogger('CoreConfig') self._setup_logger() self.env_prefix = env_prefix self.config_path = Path(config_path) if config_path else None self.config_data = default_config or {} self.config_modified = False self.log(f"📋 初始化配置管理器 | 环境前缀: {env_prefix}") # 加载配置 self.load_configuration() def _setup_logger(self): """配置日志记录器""" if not self.logger.handlers: handler = logging.StreamHandler() formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) self.logger.addHandler(handler) self.logger.setLevel(logging.INFO) self.logger.propagate = False def log(self, message: str, level: str = "info"): """记录日志""" log_levels = { "debug": self.logger.debug, "info": self.logger.info, "warning": self.logger.warning, "error": self.logger.error } log_func = log_levels.get(level.lower(), self.logger.info) log_func(message) def load_configuration(self): """加载所有配置源""" # 1. 加载环境变量 self._load_environment_vars() # 2. 加载配置文件(如果存在) if self.config_path and self.config_path.exists(): self._load_config_file() elif self.config_path: self.log(f"⚠️ 配置文件不存在: {self.config_path}", "warning") self.log(f"✅ 配置加载完成 | 条目数: {len(self.config_data)}") def _load_environment_vars(self): """加载环境变量""" self.log("🔍 加载环境变量...") load_dotenv() # 加载.env文件(如果存在) # 获取所有以指定前缀开头的环境变量 for key, value in os.environ.items(): if key.startswith(self.env_prefix): config_key = key[len(self.env_prefix):].lower() self.config_data[config_key] = self._parse_value(value) self.log(f" - 加载环境变量: {config_key} = {self._mask_secret(config_key, value)}") def _load_config_file(self): """根据文件扩展名加载配置文件""" suffix = self.config_path.suffix.lower() self.log(f"📄 加载配置文件: {self.config_path} (类型: {suffix[1:]})") try: if suffix == '.json': self._load_json_config() elif suffix in ('.ini', '.cfg'): self._load_ini_config() elif suffix == '.env': self._load_dotenv_config() else: self.log(f"❌ 不支持的配置文件类型: {suffix}", "error") except Exception as e: self.log(f"❌ 配置文件加载失败: {str(e)}", "error") def _load_json_config(self): """加载JSON配置文件""" with open(self.config_path, 'r', encoding='utf-8') as f: json_data = json.load(f) self._merge_config(json_data) def _load_ini_config(self): """加载INI配置文件""" parser = configparser.ConfigParser() parser.read(self.config_path) config_dict = {} for section in parser.sections(): section_dict = {} for key, value in parser.items(section): section_dict[key] = self._parse_value(value) config_dict[section] = section_dict self._merge_config(config_dict) def _load_dotenv_config(self): """加载.env配置文件""" # 已由load_dotenv()处理,这里只需记录 self.log(" - .env文件已加载") def _merge_config(self, new_config: Dict[str, Any]): """合并新配置到现有配置""" for key, value in new_config.items(): # 处理嵌套配置 if isinstance(value, dict) and key in self.config_data and isinstance(self.config_data[key], dict): self.config_data[key].update(value) else: self.config_data[key] = value self.log(f" - 加载配置项: {key} = {self._mask_secret(key, value)}") def _parse_value(self, value: str) -> Any: """智能解析字符串值为合适的Python类型""" # 尝试解析为布尔值 if value.lower() in ('true', 'yes', 'on'): return True if value.lower() in ('false', 'no', 'off'): return False # 尝试解析为数字 try: return int(value) except ValueError: try: return float(value) except ValueError: pass # 尝试解析为列表 if ',' in value: return [self._parse_value(item.strip()) for item in value.split(',')] # 返回原始字符串 return value def _mask_secret(self, key: str, value: Any) -> str: """对敏感信息进行掩码处理""" if 'secret' in key or 'password' in key or 'key' in key: return '******' if value else '空' if isinstance(value, list): return f"[列表, 长度: {len(value)}]" if isinstance(value, dict): return f"{{字典, 键数: {len(value)}}}" return str(value) def get(self, key: str, default: Any = None) -> Any: """ 获取配置值,支持点分路径 (如: 'database.host') :param key: 配置键名 :param default: 默认值(如果键不存在) :return: 配置值 """ keys = key.split('.') current = self.config_data for k in keys: if isinstance(current, dict) and k in current: current = current[k] else: return default return current def set(self, key: str, value: Any, persist: bool = False): """ 设置配置值 :param key: 配置键名 :param value: 配置值 :param persist: 是否持久化到配置文件 """ keys = key.split('.') current = self.config_data # 遍历创建嵌套字典 for i, k in enumerate(keys[:-1]): if k not in current or not isinstance(current[k], dict): current[k] = {} current = current[k] # 设置最终值 last_key = keys[-1] current[last_key] = value self.config_modified = True self.log(f"⚙️ 设置配置: {key} = {self._mask_secret(last_key, value)}") # 持久化到文件 if persist and self.config_path: self.save_config() def save_config(self, path: Optional[Union[str, Path]] = None): """ 保存配置到文件 :param path: 可选的自定义保存路径 """ save_path = Path(path) if path else self.config_path if not save_path: self.log("❌ 保存失败: 未指定配置文件路径", "error") return False suffix = save_path.suffix.lower() try: if suffix == '.json': with open(save_path, 'w', encoding='utf-8') as f: json.dump(self.config_data, f, indent=2, ensure_ascii=False) elif suffix in ('.ini', '.cfg'): self._save_ini_config(save_path) else: self.log(f"❌ 不支持的文件格式: {suffix}", "error") return False self.log(f"💾 配置已保存到: {save_path}") self.config_modified = False return True except Exception as e: self.log(f"❌ 配置保存失败: {str(e)}", "error") return False def _save_ini_config(self, path: Path): """保存为INI格式配置文件""" parser = configparser.ConfigParser() # 递归处理嵌套字典 def add_section(data, section_name=None): if section_name: parser.add_section(section_name) for key, value in data.items(): if isinstance(value, dict): add_section(value, f"{section_name}.{key}" if section_name else key) else: if not section_name: parser.set('DEFAULT', key, str(value)) else: parser.set(section_name, key, str(value)) add_section(self.config_data) with open(path, 'w', encoding='utf-8') as f: parser.write(f) def reload(self): """重新加载所有配置源""" self.log("🔄 重新加载配置...") self.config_data = {} self.load_configuration() def to_dict(self) -> Dict[str, Any]: """返回当前配置的完整字典""" return self.config_data.copy() def __getitem__(self, key: str) -> Any: """支持字典式访问""" return self.get(key) def __setitem__(self, key: str, value: Any): """支持字典式设置""" self.set(key, value) def __contains__(self, key: str) -> bool: """检查配置项是否存在""" return self.get(key) is not None # 测试代码 if __name__ == "__main__": logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' ) print("=" * 50) print("核心配置管理器测试") print("=" * 50) # 创建临时配置文件 config_json = """ { "database": { "host": "localhost", "port": 5432, "username": "admin", "password": "db_secret" }, "logging": { "level": "INFO", "path": "/var/log/app.log" }, "features": ["auth", "caching", "analytics"] } """ with open("test_config.json", "w") as f: f.write(config_json) # 设置环境变量 os.environ["APP_API_KEY"] = "env_secret_key" os.environ["APP_DATABASE_PORT"] = "6432" # 覆盖配置文件中的端口 # 创建配置管理器实例 config = CoreConfig( config_path="test_config.json", env_prefix="APP_", default_config={ "app_name": "MyApp", "version": "1.0.0" } ) # 测试配置获取 print("\n测试配置获取:") print(f"数据库主机: {config.get('database.host')}") print(f"数据库端口: {config.get('database.port')} (应被环境变量覆盖)") print(f"API密钥: {config.get('api_key')} (来自环境变量)") print(f"应用名称: {config.get('app_name')} (来自默认配置)") print(f"日志级别: {config.get('logging.level')}") print(f"功能列表: {config.get('features')}") # 测试配置设置 print("\n测试配置设置:") config.set("new_feature.enabled", True) config.set("database.password", "new_secret", persist=True) print(f"新功能状态: {config.get('new_feature.enabled')}") # 测试配置保存 print("\n测试配置保存:") config.save_config("test_config_saved.json") # 测试配置包含检查 print("\n测试配置包含检查:") print(f"数据库用户名存在: {'database.username' in config}") print(f"不存在的键: {'nonexistent.key' in config}") # 测试配置转字典 print("\n完整配置字典:") print(json.dumps(config.to_dict(), indent=2, ensure_ascii=False))
最新发布
08-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值