我需要在Swift代码中包含JavaScript代码才能调用signalR聊天,这可能吗?如果没有,我可以转换它吗?
sendmessage是一个按钮.
$(function () { // Declare a proxy to reference the hub. var chat = $.connection.chatHub; // Create a function that the hub can call to broadcast messages. chat.client.broadcastMessage = function (name,message) { // some code }; // Start the connection. $.connection.hub.start().done(function () { $('#sendmessage').click(function () { // Call the Send method on the hub. chat.server.send('name','message'); }); }); });
信号器代码是:
public void Send(string name,string message) { // Call the broadcastMessage method to update clients. Clients.All.broadcastMessage(name,message); }
更新#1:
改变了一点问题,所以每个@MartinR都不会混淆
你可以在Swift里面运行JavaScript!
这是一个让你入门的例子:
import JavaScriptCore let jsSource = "var testFunct = function(message) { return \"Test Message: \" + message;}" var context = JSContext() context.evaluateScript(jsSource) let testFunction = context.objectForKeyedSubscript("testFunct") let result = testFunction.callWithArguments(["the message"])
结果将是测试消息:消息.
您还可以通过调用stringByEvaluatingJavaScript(from:)或在调用evaluateJavaScript(_:completionHandler:)的WKWebView内运行UIWebView中的JavaScript代码
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。