我需要在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代码