iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >错误 org.freedesktop.DBus.Error.UnknownMethod:未知/无效方法“Notify”
  • 346
分享到

错误 org.freedesktop.DBus.Error.UnknownMethod:未知/无效方法“Notify”

2024-02-06 08:02:13 346人浏览 安东尼
摘要

问题内容 我正在尝试使用 Godbus 创建一个通知服务器,但我无法正确地将我的服务器对象导出到 dbus,并且 dbus 只能识别我的内省 xml。我按照 https://speci

问题内容

我正在尝试使用 Godbus 创建一个通知服务器,但我无法正确地将我的服务器对象导出到 dbus,并且 dbus 只能识别我的内省 xml。我按照 https://specifications.freedesktop.org/notification-spec/notification-spec-latest.html 来实现它。我还在 godbus 存储库中使用了 _example/server.go,您可能会在下面提供的服务器代码中注意到。 这是服务器代码:

package main

import (
    "fmt"
    "os"

    "GitHub.com/godbus/dbus/v5"
    "github.com/godbus/dbus/v5/introspect"
)

const xml = `

    
        
            
            
            
            
            
            
            
            
            
        

        
            
        

        
            
            
            
            
        

        
            
        

        
            
            
        
    ` + introspect.introspectdatastring + ` `

type notificationserver struct {
}

func (s *notificationserver) notify(appname string, replacesid uint32, appicon string, summary string, body string, actions []string, hints map[string]dbus.variant, expiretimeout int32) (uint32, *dbus.error) {
    fmt.printf("new notification: %s\n", body)
    return 0, nil
}

func (s *notificationserver) getcapabilities() ([]string, *dbus.error) {
    return []string{"action-icons", "actions", "body", "body-hyperlinks", "body-images", "body-markup", "icon-multi", "icon-static", "persistence", "sound"}, nil
}

func (s *notificationserver) getserverinfORMation() (string, string, string, string, *dbus.error) {
    return "antarctica", "antarctica.com", "1.0", "1.2", nil
}

func (s *notificationserver) closenotification(id uint32) *dbus.error {
    s.notificationclosed(id, 0)
    return nil
}
func (s *notificationserver) notificationclosed(id, reason uint32) {

}

func main() {
    conn, err := dbus.connectsessionbus()
    if err != nil {
        panic(err)
    }
    defer conn.close()

    reply, err := conn.requestname("com.antarctica.notification",
        dbus.nameflagdonotqueue)
    if err != nil {
        panic(err)
    }

    if reply != dbus.requestnamereplyprimaryowner {
        fmt.fprintln(os.stderr, "name already taken")
        os.exit(1)
    }
    server := notificationserver{}
    err = conn.export(server,"/org/freedesktop/notifications","org.freedesktop.notifications")
    if err != nil {
        panic(err)
    }
    conn.export(introspect.introspectable(xml), "/org/freedesktop/notifications", "org.freedesktop.dbus.introspectable")
    fmt.println("listening on com.antarctica.notification / /com/antarctica/notification ...")
    select {}
}

现在的问题是,即使客户端可以访问内省 xml:

$ gdbus introspect --session --dest com.antarctica.notification --object-path /org/freedesktop/notifications --xml

> returns xml

我无法使用我在服务器代码中编写的 org.freedesktop.notifications 方法。例如,notify 未知/无效,这对于每种方法都是相同的:

$ dbus-send --session --print-reply=literal --dest=com.antarctica.notification /org/freedesktop/Notifications org.freedesktop.Notifications.Notify

> Error org.freedesktop.DBus.Error.UnknownMethod: Unknown / invalid method 'Notify'

也在 qdbusviewer 中,当我尝试执行任何方法时,它显示“无法在接口 org.freedesktop.notifications 中的路径 /org/freedesktop/notifications 上找到方法 x”

我尝试过的:

  1. 检查 dbus 是否正在运行
  2. 检查我的服务器是否正在运行
  3. 我也尝试重新启动 dbus 服务和我的计算机
  4. 我认为notificationserver实例(服务器)根本没有被导出,但我不知道为什么


正确答案


这有效。你犯了两个错误:

  1. com.antarctica.notification
  2. func (s *notificationserver)

您必须请求“org.freedesktop.notifications”作为名称,并且不能在函数中使用指针。

  1. org.freedesktop.notifications
  2. func(通知服务器)
  3. (你也不需要内省)
package main

import (
    "fmt"
    "os"

    "github.com/godbus/dbus/v5"
)

type notificationServer struct{}

func (s notificationServer) Notify(appName string, replacesID uint32, appIcon string, summary string, body string, actions []string, hints map[string]dbus.Variant, expireTimeout int32) (uint32, *dbus.Error) {
    fmt.Printf("New notification: %s\n", body)
    return 0, nil
}

func (s notificationServer) GetCapabilities() ([]string, *dbus.Error) {
    return []string{"action-icons", "actions", "body", "body-hyperlinks", "body-images", "body-markup", "icon-multi", "icon-static", "persistence", "sound"}, nil
}

func (s notificationServer) GetServerInformation() (string, string, string, string, *dbus.Error) {
    return "antarctica", "antarctica.com", "1.0", "1.2", nil
}

func main() {
    conn, err := dbus.ConnectSessionBus()
    if err != nil {
        panic(err)
    }
    defer conn.Close()

    f := notificationServer{}
    conn.Export(f, "/org/freedesktop/Notifications", "org.freedesktop.Notifications")

    reply, err := conn.RequestName("org.freedesktop.Notifications", dbus.NameFlagDoNotQueue)
    if err != nil {
        panic(err)
    }
    if reply != dbus.RequestNameReplyPrimaryOwner {
        fmt.Fprintln(os.Stderr, "name already taken")
        os.Exit(1)
    }
    fmt.Println("Listening...")
    select {}
}

以上就是错误 org.freedesktop.DBus.Error.UnknownMethod:未知/无效方法“Notify”的详细内容,更多请关注编程网其它相关文章!

--结束END--

本文标题: 错误 org.freedesktop.DBus.Error.UnknownMethod:未知/无效方法“Notify”

本文链接: https://www.lsjlt.com/news/561293.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作